使用find()在MongoDB中search嵌套键

这将是一个愚蠢的问题,但如果我有一个这种格式的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() ,我如何findserver密钥或嵌套在另一个密钥中的任何密钥?

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

我试图以所有可能的组合引用它们,但输出一直是空白的,或...

任何build议,将不胜感激。

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

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

在你的查询中,你所要求的是headers{server: "gws"}的对象的文档,所以只有在你知道整个子文档是什么时才能工作。