Ejemplo n.º 1
0
        public override Task <IAsyncCursor <BsonDocument> > ListCollectionsAsync(ListCollectionsOptions options, CancellationToken cancellationToken)
        {
            options = options ?? new ListCollectionsOptions();
            var operation = CreateListCollectionsOperation(options);

            return(ExecuteReadOperationAsync(operation, ReadPreference.Primary, cancellationToken));
        }
        private ListCollectionsOperation CreateListCollectionsOperation(ListCollectionsOptions options)
        {
            var messageEncoderSettings = GetMessageEncoderSettings();

            return(new ListCollectionsOperation(_databaseNamespace, messageEncoderSettings)
            {
                Filter = options.Filter?.Render(_settings.SerializerRegistry.GetSerializer <BsonDocument>(), _settings.SerializerRegistry)
            });
        }
Ejemplo n.º 3
0
        //public static IEnumerable<T> GetList<T>(this IMongoDatabase db, string collectionName, IEnumerable<T> entities) {
        //  db.GetCollection<T>(collectionName).InsertMany(entities);
        //  var filterBuilder = Builders<T>.Filter;
        //  var projectionBuilder = Builders<T>.Projection;
        //  var collection = db.GetCollection<T>(collectionName);
        //  //var filter = filterBuilder.ElemMatch("carpenter..., ?"); //<--- ???
        //  var projection = projectionBuilder.Exclude("_id");
        //  var list = await collection.Find(filter).Project(projection).ToListAsync();
        //  return list;
        //}

        public static IEnumerable <BsonValue> GetCollectionsWithName(this IMongoDatabase db)
        {
            var bre  = new BsonRegularExpression("<YOUR REGEX PATTERN>");
            var copt = new ListCollectionsOptions {
                Filter = Builders <BsonDocument> .Filter.Regex("name", bre)
            };

            return(db.ListCollections(copt).ToList().Select(col => col["name"]));
        }
Ejemplo n.º 4
0
        public override Task <IAsyncCursor <BsonDocument> > ListCollectionsAsync(ListCollectionsOptions options, CancellationToken cancellationToken)
        {
            var messageEncoderSettings = GetMessageEncoderSettings();
            var operation = new ListCollectionsOperation(_databaseNamespace, messageEncoderSettings)
            {
                Filter = options == null ? null : options.Filter.Render(_settings.SerializerRegistry.GetSerializer <BsonDocument>(), _settings.SerializerRegistry)
            };

            return(ExecuteReadOperationAsync(operation, ReadPreference.Primary, cancellationToken));
        }
Ejemplo n.º 5
0
        private ListCollectionsOperation CreateListCollectionsOperation(ListCollectionsOptions options)
        {
            var messageEncoderSettings = GetMessageEncoderSettings();

            return(new ListCollectionsOperation(_databaseNamespace, messageEncoderSettings)
            {
                BatchSize = options?.BatchSize,
                Filter = options?.Filter?.Render(_settings.SerializerRegistry.GetSerializer <BsonDocument>(), _settings.SerializerRegistry, _linqProvider),
                RetryRequested = _client.Settings.RetryReads
            });
        }
Ejemplo n.º 6
0
        public void ListCollections_should_execute_a_ListCollectionsOperation(
            [Values(false, true)] bool usingSession,
            [Values(false, true)] bool async)
        {
            var session          = CreateSession(usingSession);
            var filterDocument   = BsonDocument.Parse("{ name : \"awesome\" }");
            var filterDefinition = (FilterDefinition <BsonDocument>)filterDocument;
            var options          = new ListCollectionsOptions
            {
                Filter = filterDefinition
            };
            var cancellationToken = new CancellationTokenSource().Token;

            var mockCursor = new Mock <IAsyncCursor <BsonDocument> >();

            _operationExecutor.EnqueueResult <IAsyncCursor <BsonDocument> >(mockCursor.Object);

            if (usingSession)
            {
                if (async)
                {
                    _subject.ListCollectionsAsync(session, options, cancellationToken).GetAwaiter().GetResult();
                }
                else
                {
                    _subject.ListCollections(session, options, cancellationToken);
                }
            }
            else
            {
                if (async)
                {
                    _subject.ListCollectionsAsync(options, cancellationToken).GetAwaiter().GetResult();
                }
                else
                {
                    _subject.ListCollections(options, cancellationToken);
                }
            }

            var call = _operationExecutor.GetReadCall <IAsyncCursor <BsonDocument> >();

            VerifySessionAndCancellationToken(call, session, cancellationToken);

            var op = call.Operation.Should().BeOfType <ListCollectionsOperation>().Subject;

            op.DatabaseNamespace.Should().Be(_subject.DatabaseNamespace);
            op.Filter.Should().Be(filterDocument);
        }
Ejemplo n.º 7
0
        public override Task <IAsyncCursor <BsonDocument> > ListCollectionsAsync(IClientSessionHandle session, ListCollectionsOptions options, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(session, nameof(session));
            var operation = CreateListCollectionsOperation(options);
            var effectiveReadPreference = ReadPreferenceResolver.GetEffectiveReadPreference(session, null, ReadPreference.Primary);

            return(ExecuteReadOperationAsync(session, operation, effectiveReadPreference, cancellationToken));
        }
Ejemplo n.º 8
0
 public override Task <IAsyncCursor <BsonDocument> > ListCollectionsAsync(ListCollectionsOptions options, CancellationToken cancellationToken)
 {
     return(UsingImplicitSessionAsync(session => ListCollectionsAsync(session, options, cancellationToken), cancellationToken));
 }
 /// <inheritdoc />
 public virtual Task <IAsyncCursor <BsonDocument> > ListCollectionsAsync(IClientSessionHandle session, ListCollectionsOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     throw new NotImplementedException();
 }
 /// <inheritdoc />
 public abstract Task <IAsyncCursor <BsonDocument> > ListCollectionsAsync(ListCollectionsOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
 /// <inheritdoc />
 public virtual IAsyncCursor <BsonDocument> ListCollections(ListCollectionsOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     throw new NotImplementedException();
 }
        public override IAsyncCursor <BsonDocument> ListCollections(IClientSessionHandle session, ListCollectionsOptions options, CancellationToken cancellationToken)
        {
            Ensure.IsNotNull(session, nameof(session));
            options = options ?? new ListCollectionsOptions();
            var operation = CreateListCollectionsOperation(options);

            return(ExecuteReadOperation(session, operation, ReadPreference.Primary, cancellationToken));
        }
Ejemplo n.º 13
0
        private void Initialize()
        {
            lock (Sync)
            {
                var mongoUrl = MongoUrl.Create(_connectionString);
                var mongoClient = new MongoClient(mongoUrl);
                //var server = mongoClient.GetServer();

                var database = mongoClient.GetDatabase(mongoUrl.DatabaseName);
                //var filter = new Filter(new BsonDocument("name", _collectionName));
                var findThisOne = new ListCollectionsOptions();
                findThisOne.Filter = Builders<BsonDocument>.Filter.Eq("name", _collectionName);
                var cursor = database.ListCollectionsAsync(findThisOne).Result;
                var list = cursor.ToListAsync().GetAwaiter().GetResult();
                var allCollections = list.Select(c => c["name"].AsString).OrderBy(n => n).ToList();
                if (!allCollections.Contains(_collectionName))
                {
                    var options = new CreateCollectionOptions();
                    options.Capped = true;
                    options.AutoIndexId = true;
                    options.MaxSize = _maxSize;

                    database.CreateCollectionAsync(_collectionName, options).GetAwaiter().GetResult();
                }

                _collection = database.GetCollection<ErrorModel>(_collectionName);
                //_mongoInsertOptions = new MongoInsertOptions { CheckElementNames = false };
            }
        }