将多个BSon ObjectID反序列化为C#.Net中MongoDB文档的字符串
我需要使用C#.Net将mongodb集合中所有文档的和Boss\u id的值从将多个BSon ObjectID反序列化为C#.Net中MongoDB文档的字符串,c#,mongodb,serialization,deserialization,bson,C#,Mongodb,Serialization,Deserialization,Bson,我需要使用C#.Net将mongodb集合中所有文档的0)) { _雇员=cItem; } 这是一个例外 类型属性 MongoDB.Bson.Serialization.Attributes.BsonIdAttribute只能是 适用于单个成员 请帮助我如何获取文档?请尝试下面的代码。[BsonId]是文档的id,因此在Json中它是“\u id”元素。就这么多了 public class EmployeeModel { [BsonId] [BsonRepresentatio
ObjectId
反序列化为字符串
我的收藏Employee
是(这里我只粘贴了2个文档,实际上我有超过10000个文档)
我的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;
}
私有静态IMongoClient\u客户端;
私有静态IMongoDatabase\u数据库;
_客户端=新的MongoClient();
_数据库=_client.GetDatabase(“RMS”);
var collection=_database.GetCollection(“Employee”);
BsonDocument temp=新的BsonDocument(“Emp_名称”、“Raj”);
var cItem=collection.Find(temp.ToList();
如果((cItem!=null)&&(cItem.Count>0))
{
_雇员=cItem;
}
这是一个例外
类型属性
MongoDB.Bson.Serialization.Attributes.BsonIdAttribute只能是
适用于单个成员
请帮助我如何获取文档?请尝试下面的代码。[BsonId]是文档的id,因此在Json中它是“\u id”元素。就这么多了
public class EmployeeModel
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonRepresentation(BsonType.ObjectId)]
public string Boss_Id { get; set; }
public string Emp_Name { get; set; }
}
请尝试下面的代码。[BsonId]是文档的id,因此在Json中它是“\u id”元素。就这么多了
public class EmployeeModel
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonRepresentation(BsonType.ObjectId)]
public string Boss_Id { get; set; }
public string Emp_Name { get; set; }
}
我认为该错误试图告诉您不能将两个字段用作BsonType.objecti我认为该错误试图告诉您不能将两个字段用作BsonType.objecti。你说得对。谢谢,是的。你说得对。谢谢