Advertisement
NgThanhPhuc

MongoDB_BulkWrite

Feb 13th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 KB | None | 0 0
  1. using MongoDB.Bson;
  2. using MongoDB.Bson.IO;
  3. using MongoDB.Bson.Serialization;
  4. using MongoDB.Bson.Serialization.Attributes;
  5. using MongoDB.Driver;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace M101DotNet
  12. {
  13.     class Program
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             MainAsync(args).GetAwaiter().GetResult();
  18.             Console.WriteLine("Press enter");
  19.             Console.ReadLine();
  20.  
  21.         }
  22.  
  23.         static async Task MainAsync(string[] args)
  24.         {
  25.             var client = new MongoClient();
  26.             var db = client.GetDatabase("Phucdb");
  27.             var col = db.GetCollection<BsonDocument>("widgets");
  28.             // Drop r Insert
  29.             await db.DropCollectionAsync("widgets");
  30.             var docs = Enumerable.Range(0, 10).Select(i => new BsonDocument("_id",i).Add("x",i));
  31.             await col.InsertManyAsync(docs);
  32.             //BulkWrite
  33.             // Các lệnh trong BulkWrite thực thi đồng thời ko hàng đợi
  34.             var result = col.BulkWriteAsync(new WriteModel<BsonDocument>[]
  35.             {
  36.                 new DeleteOneModel<BsonDocument>("{x:5}"),
  37.                 new DeleteOneModel<BsonDocument>("{_id:7}"),
  38.                 new UpdateManyModel<BsonDocument>("{x:{$lt:8}}","{$inc:{x:1}}")
  39.             });
  40.             // Xuat
  41.             await col.Find(new BsonDocument()).ForEachAsync(x => Console.WriteLine(x));
  42.         }
  43.        
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement