Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using MongoDB.Bson;
- using MongoDB.Bson.IO;
- using MongoDB.Bson.Serialization;
- using MongoDB.Bson.Serialization.Attributes;
- using MongoDB.Driver;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace M101DotNet
- {
- class Program
- {
- static void Main(string[] args)
- {
- MainAsync(args).GetAwaiter().GetResult();
- Console.WriteLine("Press enter");
- Console.ReadLine();
- }
- static async Task MainAsync(string[] args)
- {
- var client = new MongoClient();
- var db = client.GetDatabase("Phucdb");
- var col = db.GetCollection<Widget>("widgets");
- // Drop r Insert
- await db.DropCollectionAsync("widgets");
- var docs = Enumerable.Range(0, 10).Select(i => new Widget { Id = i, X = i });
- await col.InsertManyAsync(docs);
- //FindOneAndUpdate
- var result = await col.FindOneAndUpdateAsync<Widget>(
- x => x.X > 5,
- Builders<Widget>.Update.Inc(x => x.X, 555),
- new FindOneAndUpdateOptions<Widget, Widget>
- {
- ReturnDocument = ReturnDocument.After, // Hiện gtri trc' or sau chỉnh sữa
- Sort = Builders<Widget>.Sort.Descending(x=>x.X) // Sort gtri chỉnh sữa theo ý muốn
- }
- );
- // Xuat dòng vừa dc thực thi
- Console.WriteLine(result);
- Console.WriteLine();
- // Xuat
- await col.Find(new BsonDocument()).ForEachAsync(x => Console.WriteLine(x));
- }
- private class Widget
- {
- public int Id { get; set; }
- [BsonElement("x")]
- public int X { get; set; }
- public override string ToString()
- {
- return string.Format("Id: {0}, X: \"{1}\"", Id, X);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement