De-Serialize 多个 BSon ObjectID 到 C# .Net 中 MongoDB 文档的字符串

De-Serialize multiple BSon ObjectID to String of a MongoDB document in C# .Net

我需要 de-serialize _idBoss_id 的值 ObjectIdstring 对于 mongodb collection 中的所有文档使用 C# .Net

我的CollectionEmployee是(这里我只粘贴了2个文档,实际上我有超过10K个文档)

{
    "_id" : ObjectId("575845a713d284da0ac2ee81"),
    "Boss_id" : ObjectId("575841b313d284da0ac2ee7d"),
    "Emp_Name" : "Raj",
}

{
    "_id" : ObjectId("575845d213d284da0ac2ee82"),
    "Boss_id" : ObjectId("575841b313d284da0ac2ee7d"),
    "Emp_Name" : "Kumar"
}

我的 C# 源代码 - 模型 Class 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;
} 

Its throwing Exception

Attributes of type MongoDB.Bson.Serialization.Attributes.BsonIdAttribute can only be applied to a single member.

请帮助我如何获取文件?

试试下面的代码。 [BsonId] 是文档的 ID,因此在 Json 中它是“_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; }
}