C#:检索BSON文档数组的值 [英] C# : Retrieve array values from bson document

查看:991
本文介绍了C#:检索BSON文档数组的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我收集的MongoDB,我有一个数组项的文件。我如何获得这些数组值在C#中的字符串数组?我能得到的文件本身回来很好,但我似乎无法得到数组值。这是我到:

  QueryDocument findUser =新QueryDocument(_ ID,身份证); 
BsonDocument用户= bsonCollection.FindOne(findUser);



因此,在这种用户文件,有一个数组,我想获得和解析成一个字符串数组。该文件看起来是这样的:

  {
名字:乔恩,
secondname :铁匠,
爱:这个,那个,其他的东西]
}


解决方案

如果我正确地得到了你的问题,一种方法是:

  VAR的queryString = Query.EQ(_ ID,身份证); 
VAR resultBsons = collection.FindOne(的queryString);
VAR arrayOfStrings = resultBsons [爱] AsBsonArray.Select(P => p.AsString)。.ToArray();


In my MongoDB collection, I have a document with an array entry. How do I get these array values as a string array in 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. 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();

这篇关于C#:检索BSON文档数组的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆