C #: getting array values โ€‹โ€‹from a bson document

In my MongoDB collection, I have a document with an array entry. How to get these array values โ€‹โ€‹as an array of strings in C #? I can return the document itself, but I cannot get the values โ€‹โ€‹of the array. Here is where I can:

QueryDocument findUser = new QueryDocument("_id" , id); BsonDocument user = bsonCollection.FindOne(findUser); 

So, in this document, user has an array that I would like to get and parse into an array of strings. The document looks something like this:

 { "firstname" : "jon", "secondname" : "smith", "loves" : ["this","that","other stuff"] } 
+6
source share
1 answer

If I understand your problem correctly, one approach:

 var queryString = Query.EQ("_id", id); var resultBsons = collection.FindOne(queryString); var arrayOfStrings = resultBsons["loves"].AsBsonArray.Select(p => p.AsString).ToArray(); 
+9
source

Source: https://habr.com/ru/post/978475/


All Articles