<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      關(guān)于C#中MongoDB基礎(chǔ)操作的小例子

      ET框架最后的DB模塊,嘗試了一些MongoDB在C#中的基本操作,便于理解ET的相關(guān)方法。需要說明的一點,對象保存為文檔時,private成員不會保存,public和protected成員會被保存,可以添加[BsonIgnore]標(biāo)簽忽略掉。
      官網(wǎng)教程:https://docs.mongodb.com/drivers/csharp/

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using MongoDB.Bson;
      using MongoDB.Driver;
      
      namespace TestLambda
      {
      	class Program
      	{
      		static void Main()
      		{
      			MongoClient client = new MongoClient("mongodb://127.0.0.1:27017");
      			IMongoDatabase database = client.GetDatabase("mymongo");
      
      			var collection = database.GetCollection<BsonDocument>("mymongo3");  //獲取集合實例,如果不存在則自動創(chuàng)建,BsonDocument需要using MongoDB.Bson;
      			var document = new BsonDocument	//創(chuàng)建文檔實例
      			{
      				{ "name", "MongoDB" },
      				{ "type", "Database" },
      				{ "count", 1 },
      				{ "info", new BsonDocument
      					{
      						{ "x", 203 },
      						{ "y", 102 }
      					}}
      			};
      
      			//InsertOne插入單個文檔
      			//mongosh中使用db.mymongo3.find()可以查詢到這條記錄
      			collection.InsertOne(document);
      
      			//InsertMany插入多個文檔
      			//Linq語句:Enumerable.Range生成指定范圍內(nèi)的整數(shù)的序列;Enumerable.Select遍歷序列投影到新表單
      			var documents = Enumerable.Range(0, 10).Select(i => new BsonDocument("i", i));
      			collection.InsertMany(documents);
      
      			long count = collection.CountDocuments(new BsonDocument()); //統(tǒng)計文檔數(shù)量
      			Console.WriteLine(count);
      
      			//Find查詢結(jié)果返回首條數(shù)據(jù)
      			BsonDocument documentResult = collection.Find(new BsonDocument()).FirstOrDefault();
      			Console.WriteLine(documentResult.ToString());
      
      			//Find查詢結(jié)果轉(zhuǎn)換為集合遍歷輸出
      			List<BsonDocument> documentsResult = collection.Find(new BsonDocument()).ToList();
      			for (int i = 0; i < documentsResult.Count; i++)
      			{
      				Console.WriteLine(documentsResult[i]);
      			}
      
      			//Find查詢結(jié)果轉(zhuǎn)換為游標(biāo)遍歷輸出,配合ToEnumerable枚舉器
      			IAsyncCursor<BsonDocument> cursor = collection.Find(new BsonDocument()).ToCursor();
      			foreach (var doc in cursor.ToEnumerable())
      			{
      				Console.WriteLine(doc);
      			}
      
      			//創(chuàng)建一個過濾器傳遞給Find方法進(jìn)行查詢
      			var filter = Builders<BsonDocument>.Filter.Eq("count", 1);
      			var documentFilter = collection.Find(filter).First();
      			Console.WriteLine(documentFilter);
      
      			//==================================================
      
      			//看看直接存儲對象實例是什么內(nèi)容
      			var collectionClass1 = database.GetCollection<Entity1>("MongoClass1");
      
      			Entity1 entity1 = new Entity1(1, "Entity1");
      			collectionClass1.InsertOne(entity1);
      			#region Find輸出
      			//[ { _id: 1, Name: 'Entity1' } ]
      			#endregion
      
      			//加個嵌套試試
      			var collectionClass2 = database.GetCollection<Entity2>("MongoClass2");
      
      			Component component = new Component(10, "Component");
      			Entity2 entity2 = new Entity2(2, "Entity2", component);
      			collectionClass2.InsertOne(entity2);
      			#region Find輸出
      			//[
      			//	{
      			//		_id: 2,
      			//		Name: 'Entity2',
      			//		component: { _id: 10, Name: 'Component' }
      			//	}
      			//]
      			#endregion
      
                              database.GetCollection<Entity2>(typeof(Entity2).Name).ReplaceOne(d => d.Id == 2, entity2, new ReplaceOptions { IsUpsert = true });
      		}
      
      		public class Entity1
      		{
      			public int Id { get; set; }
      			public string Name { get; set; }
      
      			public Entity1(int id, string name)
      			{
      				Id = id;
      				Name = name;
      			}
      		}
      
      		public class Entity2
      		{
      			public int Id { get; set; }
      			public string Name { get; set; }
      			public Component component { get; set; }
      
      			public Entity2(int id, string name, Component component)
      			{
      				Id = id;
      				Name = name;
      				this.component = component;
      			}
      		}
      
      		public class Component
      		{
      			public int Id { get; set; }
      			public string Name { get; set; }
      
      			public Component(int id, string name)
      			{
      				Id = id;
      				Name = name;
      			}
      
      			public Component()
      			{
      			}
      		}
      	}
      }
      
      posted @ 2021-11-20 21:18  qianxun0975  閱讀(673)  評論(1)    收藏  舉報
      主站蜘蛛池模板: 亚洲国产99精品国自产拍| 亚洲日本欧洲二区精品| 久久se精品一区二区三区| 色猫咪av在线观看| 激情综合网激情五月我去也| 中文 在线 日韩 亚洲 欧美| 国产AV无码专区亚洲AWWW| 五月天免费中文字幕av| 久久国产乱子伦免费精品无码| 巢湖市| 最新午夜男女福利片视频| 视频一区视频二区中文字幕| 人妻aⅴ无码一区二区三区| 日韩国产欧美精品在线| 国产不卡精品视频男人的天堂| 成人午夜免费无码视频在线观看| 97在线碰| 久久99国产精品久久99小说 | 亚洲精品中文字幕在线观| 国产色无码精品视频免费| 国产免费无遮挡吃奶视频| 亚洲成人精品一区二区中| 狠狠色狠狠综合久久| аⅴ天堂中文在线网| 中文字幕日韩精品无码内射| 久久综合老鸭窝色综合久久| 精品国产一区二区三区av性色| 亚洲男女羞羞无遮挡久久丫| 亚洲综合国产伊人五月婷| 中文字幕在线观看一区二区| 久章草这里只有精品| 旬邑县| 国产精品不卡一二三区 | 丁香婷婷色综合激情五月| 久久亚洲精品成人av秋霞| 潘金莲高清dvd碟片| 精品日本免费一区二区三区| 海城市| 26uuu另类亚洲欧美日本| av永久免费网站在线观看| 欧美高清一区三区在线专区|