使用find()在MongoDB中搜索嵌套键

时间:2013-03-22 16:30:39

标签: javascript node.js mongodb database

这将是一个愚蠢的问题,但如果我有一个这种格式的Mongo对象:

{
    "url": "google.com",
    "statusCode": 301,
    "headers": {
        "location": "http://www.google.com/",
        "content-type": "text/html; charset=UTF-8",
        "date": "Fri, 22 Mar 2013 16:27:55 GMT",
        "expires": "Sun, 21 Apr 2013 16:27:55 GMT",
        "cache-control": "public, max-age=2592000",
        "server": "gws",
        "content-length": "219",
        "x-xss-protection": "1; mode=block",
        "x-frame-options": "SAMEORIGIN"
    }
}

使用db.collections.find(),如何找到server密钥或嵌套在另一个密钥中的任何密钥?

我试过了db.collections.find({headers:{server:"gws"}})

我尝试过以所有可能的组合引用它们,但输出始终为空白,或...

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:16)

你必须使用点符号来获得你想要的东西。它看起来像是:

db.collections.find({"headers.server":"gws"})

在您的查询中,您要求的是headers是一个看起来像{server: "gws"}的对象的文档,这样只有在您知道整个的情况下才能工作子文档是。