简体   繁体   English

C#:从bson文档中检索数组值

[英]C# : Retrieve array values from bson document

In my MongoDB collection, I have a document with an array entry. 在我的MongoDB集合中,我有一个带有数组条目的文档。 How do I get these array values as a string array in C#? 如何在C#中将这些数组值作为字符串数组获取? I can get the document itself back fine but I can't seem to get the array values. 我可以让文档本身恢复正常,但我似乎无法获得数组值。 This is where I'm up to : 这是我要做的事情:

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

So in this user document, there is an array that I'd like to get and parse into a string array. 所以在这个user文档中,有一个我想要获取并解析为字符串数组的数组。 The document looks something like this : 该文档看起来像这样:

{
  "firstname" : "jon",
  "secondname" : "smith",
  "loves" : ["this","that","other stuff"]
}

If I got your problem correctly, One approach is : 如果我的问题正确,一种方法是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM