【发布时间】:2016-06-10 03:09:16
【问题描述】:
我需要将 _id 和 Boss_id 的值从 ObjectId 反序列化为 string 对于使用 C# .Net 的 mongodb 集合中的所有文档
我的收藏 Employee 是(这里我只粘贴了 2 个文档,实际上我有超过 10K 的文档)
{
"_id" : ObjectId("575845a713d284da0ac2ee81"),
"Boss_id" : ObjectId("575841b313d284da0ac2ee7d"),
"Emp_Name" : "Raj",
}
{
"_id" : ObjectId("575845d213d284da0ac2ee82"),
"Boss_id" : ObjectId("575841b313d284da0ac2ee7d"),
"Emp_Name" : "Kumar"
}
我的 C# 源代码 - 模型类 EmployeeModel
public class EmployeeModel
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Boss_Id { get; set; }
public string Emp_Name { get; set; }
}
我的 C# MongoDB 代码:
private static IMongoClient _client;
private static IMongoDatabase _database;
_client = new MongoClient();
_database = _client.GetDatabase("RMS");
var collection = _database.GetCollection<EmployeeModel>("Employee");
BsonDocument temp = new BsonDocument("Emp_Name", "Raj");
var cItem = collection.Find(temp).ToList();
if ((cItem != null) && (cItem.Count > 0))
{
_EmpList = cItem;
}
抛出异常
类型的属性 MongoDB.Bson.Serialization.Attributes.BsonIdAttribute 只能是 应用于单个成员。
请帮助我如何获取文件?
【问题讨论】:
-
我认为错误试图告诉您不能使用 2 个字段作为 BsonType.ObjectId
标签: c# mongodb serialization deserialization bson