[英]Retrieve the value of a mongoDB field in c#
I am looking to retrieve the value of a field (_id) from MongoDB document.我希望从 MongoDB 文档中检索字段 (_id) 的值。
which looks something like this -看起来像这样 -
{
"_id" : "1b27413b-0eac-4986-bb17-d31d9ca666c8",
"ReferenceNumber" : "8708753",
"AppointmentStart" : ISODate("2018-11-11T10:00:00.000+00:00"),
"AppointmentEnd" : ISODate("2018-11-11T10:30:00.000+00:00"),
"PropertyValue" : "4500000",
"Value" : "1889000",
"Status" : "ISSUED",
"AdviserId" : "publishSR",
"ErqSetId" : "5d3acd58d174f71738cef81a",
"ErqCompletionStatus" : "Completed",
"Customer1Id" : "ee73baf3-913d-4c09-93a3-b59c6f6a542e",
"Customer2Id" : null,
"ErqCompletionDateTime" : ISODate("2019-08-02T13:37:17.092+01:00"),
"IsProductRecommended" : true,
"RecommendedProductId" : "66a1468d-f155-45a7-9f70-f29ac1bef930",
"ProductRecommendedDateTime" : ISODate("2019-08-05T16:00:31.413+01:00"),
"ResultSetId" : "4e70336b-39f6-4312-8e7a-c0f26f5a603d"
}
I have written code which returns the field as list -> result {{ "_id" : "1b27413b-0eac-4986-bb17-d31d9ca666c8" }}
我编写了将字段作为list -> result {{ "_id" : "1b27413b-0eac-4986-bb17-d31d9ca666c8" }}
返回的代码list -> result {{ "_id" : "1b27413b-0eac-4986-bb17-d31d9ca666c8" }}
var db = MongoApi.MongoClient.GetDatabase(MongoApi.DatabaseCases);
var collection = db.GetCollection<BsonDocument>(MongoApi.CollectionCases);
var filter = Builders<BsonDocument>.Filter.Eq("AdviserId", "publishSR");
var projection = Builders<BsonDocument>.Projection.Include("_id");
var result = collection.Find<BsonDocument>(filter).Project(projection).ToList();
How can I retrieve just the value of the field from result (_id - 1b27413b-0eac-4986-bb17-d31d9ca666c8)
as string?如何仅从结果(_id - 1b27413b-0eac-4986-bb17-d31d9ca666c8)
检索字段的值作为字符串?
Use the overloaded indexer on BsonDocument;在 BsonDocument 上使用重载的索引器;
result.First()["_id"].AsString();
Though it would be better to keep it an objectID and use AsObjectId() instead of AsString();虽然最好保留一个 objectID 并使用 AsObjectId() 而不是 AsString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.