コード例 #1
0
        public async Task <T> AddAsync(T model, InsertOneOptions options)
        {
            await this.Collection.InsertOneAsync(model, options, new CancellationToken());

            return(model);
        }
コード例 #2
0
 /// <summary>
 /// 新增一条记录
 /// </summary>
 /// <param name="document"></param>
 /// <param name="options"></param>
 public void Add(T document, InsertOneOptions options = null)
 {
     Add(collectionTypeName, document, options);
 }
コード例 #3
0
ファイル: MongoDBMocks.cs プロジェクト: danielmltn/BookBinder
 public override void InsertOne(Book document, InsertOneOptions options = null, CancellationToken cancellationToken = default)
 {
 }
コード例 #4
0
 public void InsertOne(TModel document, InsertOneOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     _collection.InsertOne(document, options, cancellationToken);
 }
コード例 #5
0
#pragma warning enable

        public Task InsertOneAsync(TModel document, InsertOneOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            return(_collection.InsertOneAsync(document, options, cancellationToken));
        }
コード例 #6
0
 public abstract void InsertOne(TDocument document, InsertOneOptions options = null, CancellationToken cancellationToken = default(CancellationToken));
コード例 #7
0
 public Task InsertOneAsync(TDocument document, InsertOneOptions options = null, CancellationToken cancellationToken = default)
 => _collection.InsertOneAsync(_getSession(), document, options, cancellationToken);
コード例 #8
0
 public async Task InsertOneAsync(T document, InsertOneOptions options = null, CancellationToken cancellationToken = new CancellationToken()) =>
 await _semaphore.AddRequest(_base.InsertOneAsync(document, options, cancellationToken));
コード例 #9
0
 public async Task InsertOneAsync(IClientSessionHandle session, T document, InsertOneOptions options = null, CancellationToken cancellationToken = new CancellationToken()) =>
 await _semaphore.AddRequest(_base.InsertOneAsync(session, document, options, cancellationToken));
コード例 #10
0
 public void InsertOne(T document, InsertOneOptions options = null, CancellationToken cancellationToken = new CancellationToken()) =>
 _base.InsertOne(document, options, cancellationToken);
コード例 #11
0
 public void InsertOne(IClientSessionHandle session, T document, InsertOneOptions options = null, CancellationToken cancellationToken = new CancellationToken()) =>
 _base.InsertOne(session, document, options, cancellationToken);
コード例 #12
0
 public void InsertOne(TDocument document, InsertOneOptions options = null, CancellationToken cancellationToken = default)
 {
     throw new NotImplementedException();
 }
コード例 #13
0
        public void SearchesAndInsertions()
        {
            ModelContext modelContext  = ModelContext.Create(new ConfigFileConfigurationRepository(), new AppConfigConnectionStringRepository());
            var          boroughFilter = Builders <RestaurantDb> .Filter.Eq(r => r.Borough, "Brooklyn");

            var idSortDefinition = Builders <RestaurantDb> .Sort.Ascending(r => r.Id);

            var restaurantsInBrooklyn = modelContext.Restaurants.Find(boroughFilter).Limit(5).Sort(idSortDefinition).ToList();
            var cuisineFilter         = Builders <RestaurantDb> .Filter.Eq(r => r.Cuisine, "Delicatessen");

            var cuisineAndBoroughFilter            = boroughFilter & cuisineFilter;
            var cuisineAndBoroughFilterAlternative = Builders <RestaurantDb> .Filter.And(boroughFilter, cuisineFilter);

            var firstRes = modelContext.Restaurants.Find(cuisineAndBoroughFilterAlternative).FirstOrDefault();

            Console.WriteLine(firstRes);
            var restaurants = modelContext.Restaurants.FindSync <RestaurantDb>(boroughFilter).ToList();
            var restaurant  = modelContext.Restaurants.FindSync <RestaurantDb>(boroughFilter).FirstOrDefault();

            var firstResWithLinq = modelContext.Restaurants.Find(r => r.Borough == "Brooklyn" && r.Cuisine == "Delicatessen").FirstOrDefault();

            Console.WriteLine(firstResWithLinq);

            var arrayFilterGradeA = Builders <RestaurantDb> .Filter.ElemMatch(r => r.Grades, g => g.Grade == "A");

            var arrayFilterGradeB = Builders <RestaurantDb> .Filter.ElemMatch(r => r.Grades, g => g.Grade == "B");

            var arrayFilterGradeC = Builders <RestaurantDb> .Filter.ElemMatch(r => r.Grades, g => g.Grade == "C");

            var arrayFilterWithAllGrades = arrayFilterGradeA & arrayFilterGradeB & arrayFilterGradeC;
            var firstResWithAllGrades    = modelContext.Restaurants.Find(arrayFilterWithAllGrades).FirstOrDefault();

            Console.WriteLine(firstResWithAllGrades);

            Console.WriteLine(restaurant);
            FindOptions findOpts = new FindOptions();

            ZipCodeDb firstInMassachusetts = modelContext.ZipCodes.FindSync(z => z.State == "MA").FirstOrDefault();

            Console.WriteLine(firstInMassachusetts);

            ZipCodeDb    firstZip        = modelContext.ZipCodes.Find(z => true).FirstOrDefault();
            RestaurantDb firstRestaurant = modelContext.Restaurants.Find(r => true).FirstOrDefault();

            List <ZipCodeDb>    allZipCodes    = modelContext.ZipCodes.Find(z => true).ToList();
            List <RestaurantDb> allRestaurants = modelContext.Restaurants.Find(r => true).ToList();

            Console.WriteLine(firstZip);
            Console.WriteLine(firstRestaurant);

            var zipSortByState = Builders <ZipCodeDb> .Sort.Ascending(z => z.State);

            var zipSortByName = Builders <ZipCodeDb> .Sort.Descending(z => z.City);

            var combinedSort = Builders <ZipCodeDb> .Sort.Combine(zipSortByState, zipSortByName);

            FindOptions fo = new FindOptions();

            var first100zips = modelContext.ZipCodes.Find(z => true).Limit(100).Sort(combinedSort).ToList();

            var zipCombinedSortAlternative = Builders <ZipCodeDb> .Sort.Ascending(z => z.State).Descending(z => z.City);

            var first100zipsAlternative = modelContext.ZipCodes.Find(z => true).Limit(100).Sort(zipCombinedSortAlternative).ToList();

            var first100zipsLinqSolution = modelContext.ZipCodes.Find(z => true).Limit(100).SortBy(z => z.State).ThenByDescending(z => z.City).ToList();

            RestaurantDb newRestaurant = new RestaurantDb();

            newRestaurant.Address = new RestaurantAddressDb()
            {
                BuildingNr  = "120",
                Coordinates = new double[] { 22.82, 99.12 },
                Street      = "Whatever",
                ZipCode     = 123456
            };
            newRestaurant.Borough = "Somewhere in Thailand";
            newRestaurant.Cuisine = "Thai";
            newRestaurant.Grades  = new List <RestaurantGradeDb>()
            {
                new RestaurantGradeDb()
                {
                    Grade = "A", InsertedUtc = DateTime.UtcNow, Score = "7"
                },
                new RestaurantGradeDb()
                {
                    Grade = "B", InsertedUtc = DateTime.UtcNow, Score = "4"
                }
            };
            newRestaurant.Id   = 883738291;
            newRestaurant.Name = "RandomThai";

            modelContext.Restaurants.InsertOne(newRestaurant);

            RestaurantDb newPakistaniRestaurant = new RestaurantDb();

            newPakistaniRestaurant.Address = new RestaurantAddressDb()
            {
                BuildingNr  = "12A",
                Coordinates = new double[] { 31.135, 71.24 },
                Street      = "New Street",
                ZipCode     = 9877654
            };
            newPakistaniRestaurant.Borough = "Somewhere in Pakistan";
            newPakistaniRestaurant.Cuisine = "Pakistani";
            newPakistaniRestaurant.Grades  = new List <RestaurantGradeDb>()
            {
                new RestaurantGradeDb()
                {
                    Grade = "A", InsertedUtc = DateTime.UtcNow, Score = "9"
                },
                new RestaurantGradeDb()
                {
                    Grade = "C", InsertedUtc = DateTime.UtcNow, Score = "3"
                }
            };
            newPakistaniRestaurant.Id   = 457656745;
            newPakistaniRestaurant.Name = "PakistaniKing";

            RestaurantDb newMexicanRestaurant = new RestaurantDb();

            newMexicanRestaurant.Address = new RestaurantAddressDb()
            {
                BuildingNr  = "2/C",
                Coordinates = new double[] { 24.68, -100.9 },
                Street      = "Mexico Street",
                ZipCode     = 768324523
            };
            newMexicanRestaurant.Borough = "Somewhere in Mexico";
            newMexicanRestaurant.Cuisine = "Mexican";
            newMexicanRestaurant.Grades  = new List <RestaurantGradeDb>()
            {
                new RestaurantGradeDb()
                {
                    Grade = "B", InsertedUtc = DateTime.UtcNow, Score = "10"
                }
            };
            newMexicanRestaurant.Id   = 457656745;
            newMexicanRestaurant.Name = "MexicanKing";

            List <RestaurantDb> newRestaurants = new List <RestaurantDb>()
            {
                newPakistaniRestaurant,
                newMexicanRestaurant
            };
            InsertOneOptions opts = new InsertOneOptions();

            modelContext.Restaurants.InsertMany(newRestaurants);

            Console.ReadKey();
        }
コード例 #14
0
        protected Task AddModelAsync <T>(string collectionName, T item, InsertOneOptions options = null)
        {
            var collection = Collection <T>(collectionName);

            return(collection.InsertOneAsync(item, options));
        }
コード例 #15
0
 /// <inheritdoc/>
 public void InsertOne(IClientSessionHandle session, T document, InsertOneOptions options = null, CancellationToken cancellationToken = default)
 {
     throw new System.NotImplementedException();
 }
コード例 #16
0
        public override void InsertOne(TDocument document, InsertOneOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            var bsonDocument = SerializeDocument(document);

            _documents.Add(bsonDocument);
        }
コード例 #17
0
 /// <inheritdoc/>
 public async Task InsertOneAsync(T document, InsertOneOptions options = null, CancellationToken cancellationToken = default)
 {
     await _jsRuntime.Invoke <object>($"{MiniMongoDatabase._globalObject}.database.{_collectionName}.upsert", document).ConfigureAwait(false);
 }
コード例 #18
0
 public void InsertOne(T document, InsertOneOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     //
 }
コード例 #19
0
 public Task InsertOneAsync(TDocument document, InsertOneOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     InsertOne(document, options, cancellationToken);
     return(Task.CompletedTask);
 }
コード例 #20
0
 public Task InsertOneAsync(T document, InsertOneOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     throw new NotImplementedException();
 }
コード例 #21
0
 public Task InsertOneAsync(IClientSessionHandle session, TDocument document, InsertOneOptions options = null, CancellationToken cancellationToken = default)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// 插入单条
 /// </summary>
 /// <typeparam name="T">文档类型</typeparam>
 /// <param name="session">会话句柄(作用于事务)</param>
 /// <param name="document">文档对象</param>
 /// <param name="options">插入操作设置</param>
 /// <param name="cancellationToken">取消操作设置</param>
 public void Add <T>(IClientSessionHandle session, T document, InsertOneOptions options = null, CancellationToken cancellationToken = default) where T : BaseMongoEntity
 {
     Database.GetCollection <T>(typeof(T).Name).InsertOne(session, document, options, cancellationToken);
 }
コード例 #23
0
 public void InsertOne(IClientSessionHandle session, TModel document, InsertOneOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     _collection.InsertOne(session, document, options, cancellationToken);
 }
 /// <summary>
 /// 根据主文档的默认id动态的创建附文档的集合,集合名称规则:"{附文档名}_{主文档默认id}"
 /// </summary>
 /// <typeparam name="TForeign">文档类型</typeparam>
 /// <typeparam name="T">文档类型</typeparam>
 /// <param name="session">会话句柄(作用于事务)</param>
 /// <param name="foreignDocument">上级文档</param>
 /// <param name="document">文档对象</param>
 public void DynamicCollectionAdd <TForeign, T>(IClientSessionHandle session, TForeign foreignDocument, T document, InsertOneOptions options = null, CancellationToken cancellationToken = default)
     where TForeign : BaseMongoEntity
     where T : BaseMongoEntity
 {
     Database.GetCollection <T>($"{typeof(T).Name}_{foreignDocument.Id}").InsertOne(session, document, options, cancellationToken);
 }
コード例 #25
0
 public Task InsertOneAsync(IClientSessionHandle session, TModel document, InsertOneOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(_collection.InsertOneAsync(session, document, options, cancellationToken));
 }
コード例 #26
0
        /// <summary>
        /// Adds the new entity in the repository.
        /// </summary>
        /// <param name="entity">The entity T.</param>
        /// <param name="options">Optional insert options</param>
        /// <param name="cancellationToken">Optional threading cancellation token</param>
        /// <returns>The added entity including its new ObjectId.</returns>
        public async virtual Task <T> AddAsync(T entity, InsertOneOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            await this.collection.InsertOneAsync(entity, options, cancellationToken);

            return(entity);
        }
コード例 #27
0
 public Task AddAsync(T document, InsertOneOptions options = null, CancellationToken cancellationToken = default)
 {
     return(AddAsync(collectionTypeName, document, options, cancellationToken));
 }
コード例 #28
0
 /// <summary>
 /// Adds the new entity in the repository.
 /// </summary>
 /// <param name="entity">The entity T.</param>
 /// <param name="options">Optional insert options</param>
 /// <param name="cancellationToken">Optional threading cancellation token</param>
 /// <returns>The added entity including its new ObjectId.</returns>
 public virtual T Add(T entity, InsertOneOptions options = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     this.collection.InsertOne(entity, options, cancellationToken);
     return(entity);
 }
コード例 #29
0
 public virtual void Insert(T document, InsertOneOptions updateOptions)
 {
     _collection.InsertOne(document);
 }
コード例 #30
0
 public T Add(T model, InsertOneOptions options, CancellationToken cancellationToken)
 {
     this.Collection.InsertOne(model, options, cancellationToken);
     return(model);
 }