如果我已经将以下json加载到BSON文档中:
{
    "contact":
    { "firstname":"Pete"
        ,"surname":"Jones"
        ,"company":[{"name":"Virgin","notes":"some virgin notes"},{"name":"IBM","notes":"a great big IT company"}]
    }
    ,"response":
    {
        "_id":"123"
        ,"profileid":"567"
        ,"localdate":"12 Apr 2011 14:34:23"
    }
}我可以使用下面的命令来检测给定的元素是否存在:
if (suppliedDoc.Contains("_id"))但是我不能使用下面的语法来处理嵌套元素:
if (suppliedDoc.Contains("response._id"))寻址嵌套元素的正确语法是什么?有没有更好的方法来检测根元素或嵌套元素的存在?我使用的是官方的C#驱动。谢谢。
发布于 2011-04-20 19:28:30
你可能在找像这样的东西
if (suppliedDoc.Contains("response")
    && suppliedDoc["reponse"].AsBsonDocument.Contains("_id"))
{
    //...
}我同意这有点尴尬。
不过,在BsonDocument上编写一个扩展方法并不费力,该方法接受一个带有标点符号的字符串,按.拆分它,然后使用上面显示的方法向下钻取。
https://stackoverflow.com/questions/5729009
复制相似问题