这将是一个愚蠢的问题,但如果我有一个这种格式的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"}})
我尝试过以所有可能的组合引用它们,但输出始终为空白,或...
任何建议都将不胜感激。
答案 0 :(得分:16)
你必须使用点符号来获得你想要的东西。它看起来像是:
db.collections.find({"headers.server":"gws"})
在您的查询中,您要求的是headers
是一个看起来像{server: "gws"}
的对象的文档,这样只有在您知道整个的情况下才能工作子文档是。