2013-03-22 16 views
7

這將是一個愚蠢的問題,但如果我有一個蒙戈對象,它是這種格式:使用find()來搜索嵌套鍵MongoDB中

{ 
    "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"}})

我已經嘗試了所有可能的組合引述他們,但輸出一直是空白,或...

任何建議,將不勝感激。

回答

14

你必須使用點符號來得到你要找的東西。它看起來像:

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

在您的查詢,什麼你問的是文件,其中headers是一個對象,看起來像{server: "gws"},所以,只有當你知道整個子文檔是什麼工作。