ET框架最后的DB模塊,嘗試了一些MongoDB在C#中的基本操作,便於理解ET的相關方法。需要說明的一點,對象保存為文檔時,private成員不會保存,public和protected成員會被保存,可以添加[BsonIgnore]標簽忽略掉。
官網教程: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"); //獲取集合實例,如果不存在則自動創建,BsonDocument需要using MongoDB.Bson;
var document = new BsonDocument //創建文檔實例
{
{ "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生成指定范圍內的整數的序列;Enumerable.Select遍歷序列投影到新表單
var documents = Enumerable.Range(0, 10).Select(i => new BsonDocument("i", i));
collection.InsertMany(documents);
long count = collection.CountDocuments(new BsonDocument()); //統計文檔數量
Console.WriteLine(count);
//Find查詢結果返回首條數據
BsonDocument documentResult = collection.Find(new BsonDocument()).FirstOrDefault();
Console.WriteLine(documentResult.ToString());
//Find查詢結果轉換為集合遍歷輸出
List<BsonDocument> documentsResult = collection.Find(new BsonDocument()).ToList();
for (int i = 0; i < documentsResult.Count; i++)
{
Console.WriteLine(documentsResult[i]);
}
//Find查詢結果轉換為游標遍歷輸出,配合ToEnumerable枚舉器
IAsyncCursor<BsonDocument> cursor = collection.Find(new BsonDocument()).ToCursor();
foreach (var doc in cursor.ToEnumerable())
{
Console.WriteLine(doc);
}
//創建一個過濾器傳遞給Find方法進行查詢
var filter = Builders<BsonDocument>.Filter.Eq("count", 1);
var documentFilter = collection.Find(filter).First();
Console.WriteLine(documentFilter);
//==================================================
//看看直接存儲對象實例是什么內容
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()
{
}
}
}
}