C#中如何將MongoDB->RunCommand結(jié)果映射到業(yè)務(wù)類的方法總結(jié)
前言
一直沒實(shí)際用過MongoDB,最近有個(gè)項(xiàng)目中用了用,踩了一些坑。這篇文章會(huì)介紹將MongoDB->RunCommand結(jié)果映射到業(yè)務(wù)類時(shí)碰到的問題,以及對各種方法的探索。
Collection中的數(shù)據(jù)是這樣的:

使用find命令查詢數(shù)據(jù):
db.runCommand({"find":"test", limit:2, sort:{AddTime:-1}})
查詢返回的數(shù)據(jù)結(jié)構(gòu)是這樣的,需要的數(shù)據(jù)在firstBatch中:
{
"cursor" : {
"firstBatch" : [
{
"_id" : ObjectId("5ad0042944fd3929db6b869f"),
"Name" : "李四",
"AddTime" : ISODate("2018-04-12T01:13:18.965Z")
},
{
"_id" : ObjectId("5ad003e844fd3929db6b869e"),
"Name" : "張三",
"AddTime" : ISODate("2018-04-02T01:11:18.965Z")
}
],
"id" : NumberLong(0),
"ns" : "test.test"
},
"ok" : 1.0
}
下面將在C#中運(yùn)行find命令來執(zhí)行查詢,并將結(jié)果映射到自定義的PersonInfo類。
private class PersonInfo
{
public string Id { get; set; }
public string Name { get; set; }
public DateTime AddTime { get; set; }
}
需要注意PersonInfo中的屬性Id和Document中的名稱_id有些不一樣,需要進(jìn)行映射。因?yàn)椴煌姆葱蛄谢椒?,采用的方式也?huì)不同,所以下面會(huì)有相關(guān)介紹。
使用Json.NET
因?yàn)镴son.NET用的比較多,基本思路就是用它來反序列化Bson,但是Json和Bson是不同的,不能用JsonConvert。
不過Json.NET提供了一個(gè)新的包Newtonsoft.Json.Bson來解析Bson數(shù)據(jù),并且提供了一個(gè)例子。
https://www.newtonsoft.com/json/help/html/DeserializeFromBson.htm
這個(gè)例子有點(diǎn)過時(shí)了,里邊用的BsonReader我已替換為BsonDataReader。
byte[] data = Convert.FromBase64String("MQAAAAJOYW1lAA8AAABNb3ZpZSBQcmVtaWVyZQAJU3RhcnREYXRlAMDgKWE8AQAAAA==");
MemoryStream ms = new MemoryStream(data);
using (BsonDataReader reader = new BsonDataReader(ms))
{
JsonSerializer serializer = new JsonSerializer();
Event e = serializer.Deserialize<Event>(reader);
Console.WriteLine(e.Name);
// Movie Premiere
}
看樣子有個(gè)byte數(shù)組就可以了,然后再看BsonDocument正好有個(gè)方法ToBson,獲取到的就是byte[]。
思路:先將RunCommand的結(jié)果映射為BsonDocument,然后查找到firstBatch,然后將其ToBson,然后使用Newtonsoft.Json.Bson反序列化。
一切看起來很easy!
馬上碼起來:
var findCommand = BsonDocument.Parse("{\"find\":\"test\", limit:2, sort:{AddTime:-1}}");
var findResult = database.RunCommand<BsonDocument>(findCommand)
.GetElement("cursor").Value.ToBsonDocument()
.GetElement("firstBatch").ToBsonDocument()
.ToBson();
var personList = DeserializeBson<PersonList>(findResult);
數(shù)組不能是Bson的根元素,所以這里定義了一個(gè)PersonList類:
private class PersonList
{
public string Name { get; set; }
[JsonProperty(PropertyName = "Value")]
public PersonInfo[] List { get; set; }
}
需要注意的是上邊代碼中 GetElement(“firstBatch”).ToBsonDocument() 返回的數(shù)據(jù)格式是這樣的:

這里用了JsonProperty將Value映射為List。
還有Person類中的屬性名稱和document中的也有不同,也需要映射:
private class PersonInfo
{
[JsonProperty(PropertyName = "_id")]
[JsonConverter(typeof(ObjectIdConverter))]
public string Id { get; set; }
public string Name { get; set; }
public DateTime AddTime { get; set; }
}
這里還用了一個(gè) JsonConverter(typeof(ObjectIdConverter)) ,因?yàn)镺bjectId不能直接轉(zhuǎn)換為string,所以定義了一個(gè)ObjectIdConverter類:
public class ObjectIdConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(ObjectId);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.Value != null)
{
var value = (byte[])reader.Value;
var result = BitConverter.ToString(value).Replace("-", string.Empty).ToLowerInvariant();
return result;
}
return string.Empty;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
還有一個(gè)重要的方法,反序列化:
public static T DeserializeBson<T>(byte[] data)
{
MemoryStream ms = new MemoryStream(data);
using (BsonDataReader reader = new BsonDataReader(ms, false, DateTimeKind.Local))
{
JsonSerializer serializer = new JsonSerializer();
T e = serializer.Deserialize<T>(reader);
return e;
}
}
現(xiàn)在運(yùn)行程序看看吧:

但是感覺好復(fù)雜,過了一彎又一彎。
使用MongoDB .NET Driver內(nèi)置
話說這么常用的功能,SDK應(yīng)該內(nèi)置了才對,何必舍近求遠(yuǎn),。
既然RunCommand可以傳遞一個(gè)類型,那么SDK應(yīng)該是支持反序列化自定義類型的。
嘗試根據(jù)返回的結(jié)果定義一個(gè)新的類:
[BsonIgnoreExtraElements]
private class FindCommandResult
{
[BsonElement("cursor")]
public ResultCursor Cursor { get; set; }
}
[BsonIgnoreExtraElements]
private class ResultCursor
{
[BsonElement("firstBatch")]
public PersonInfo[] Batch { get; set; }
}
private class PersonInfo
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string Name { get; set; }
public DateTime AddTime { get; set; }
}
BsonIgnoreExtraElements、BsonElement、BsonId、BsonRepresentation這些都是SDK內(nèi)置的Attribute,相關(guān)作用大家應(yīng)該能夠一目了然。
再看看查詢這塊的代碼:
var findCommand = BsonDocument.Parse("{\"find\":\"test\", limit:1, sort:{AddTime:-1}}");
var findResult = database.RunCommand<FindCommandResult>(findCommand);
代碼跑起來:

這個(gè)方式相比Json.NET更直接,更簡單。
使用查找賦值的方式
為了反序列化,適應(yīng)MongoDB,定義了一些沒有業(yè)務(wù)意義的類型,加了很多的屬性注解,感覺還不夠直接顯性。
也許只需要數(shù)據(jù)中的某幾個(gè)字段,或者根本就沒必要定義類型,只需要放到一個(gè)列表中。
又查看了BsonDocument的定義,發(fā)現(xiàn)可以在運(yùn)行命令時(shí)先反序列化為BsonDocument,然后再根據(jù)返回的數(shù)據(jù)結(jié)構(gòu)使用GetElement獲取相關(guān)字段的值。
代碼如下:
var findCommand = BsonDocument.Parse("{\"find\":\"test\", limit:2, sort:{AddTime:-1}}");
var findResult = database.RunCommand<BsonDocument>(findCommand)
.GetElement("cursor").Value.ToBsonDocument()
.GetElement("firstBatch").Value.AsBsonArray.Select(d =>
{
var dbd = d.AsBsonDocument;
return new PersonInfo()
{
Id = dbd.GetElement("_id").Value.AsObjectId.ToString(),
AddTime = dbd.GetElement("AddTime").Value.ToLocalTime(),
Name = dbd.GetElement("Name").Value.ToString(),
};
}).ToList();
運(yùn)行后直接返回List<PersonInfo> ,更貼近業(yè)務(wù)需求。
這是本文中最簡單的方式了。
如果做的更通用點(diǎn),可以在這里通過反射自動(dòng)實(shí)例化相關(guān)類型,不過這不如直接使用SDK內(nèi)置的反序列化方式了。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- C#簡單操作MongoDB的步驟全紀(jì)錄
- Mongodb在CSharp里實(shí)現(xiàn)Aggregate實(shí)例
- MongoDB實(shí)現(xiàn)基于關(guān)鍵詞的文章檢索功能(C#版)
- C#中使用1.7版本驅(qū)動(dòng)操作MongoDB簡單例子
- MongoDB入門教程之C#驅(qū)動(dòng)操作實(shí)例
- C# 對MongoDB 進(jìn)行增刪改查的簡單操作實(shí)例
- 關(guān)于C#生成MongoDB中ObjectId的實(shí)現(xiàn)方法
- C#基于Mongo的官方驅(qū)動(dòng)手?jǐn)]一個(gè)Super簡易版MongoDB-ORM框架
相關(guān)文章
C#?Winform實(shí)現(xiàn)圓角無鋸齒按鈕
這篇文章主要介紹了C#?Winform實(shí)現(xiàn)圓角無鋸齒按鈕,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-08-08
winform中的ListBox和ComboBox綁定數(shù)據(jù)用法實(shí)例
這篇文章主要介紹了winform中的ListBox和ComboBox綁定數(shù)據(jù)用法,實(shí)例分析了將集合數(shù)據(jù)綁定到ListBox和ComboBox控件的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
C#實(shí)現(xiàn)圖表中鼠標(biāo)移動(dòng)并顯示數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)圖表中鼠標(biāo)移動(dòng)并顯示數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

