I am looking to retrieve the value of a field (_id) from MongoDB document.
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" }}
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?
Use the overloaded indexer on BsonDocument;
result.First()["_id"].AsString();
Though it would be better to keep it an objectID and use AsObjectId() instead of AsString();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.