2012-10-14 61 views
2

通常,MongoDB不會讓屬性名稱中包含點。例如:如何查詢帶點的屬性?

db.books.insert({"protagonist.name": "Guy Montog"}); 

失敗,出現錯誤:

uncaught exception: can't have . in field names [protagonist.name] 

不過,我碰到一個情況,即屬性名稱確實有點跑去。這可以發生在system.profile集合中。這裏有一個例子:

db.setProfilingLevel(2); 
db.books.insert({protagonist: "Guy Montog", antecedents: 
    [{title: "The Fireman"}, {title: "Bright Phoenix"}]}); 
db.books.find({"antecedents.title": "The Fireman"}) 

現在如果我看system.profile集合,我看到以下記錄:

> db.system.profile.findOne({op: "query"}) 
{ 
    "ts" : ISODate("2012-10-14T00:05:49.896Z"), 
    "op" : "query", 
    "ns" : "wordswing.books", 
    "query" : { 
     "antecedents.title" : "The Fireman" 
    }, 
    "nscanned" : 1, 
    "nreturned" : 1, 
    "responseLength" : 153, 
    "millis" : 0, 
    "client" : "127.0.0.1", 
    "user" : "" 
} 

假設我想查詢該查詢的「來路system.profile文件。標題」?這似乎是一個問題,因爲屬性名稱中有一個點。

我嘗試了所有的以下內容:

db.system.profile.find({'query.antecedents.title': 'The Fireman'}) 
db.system.profile.find({'query.antecedents\.title': 'The Fireman'}) 
db.system.profile.find({"query.antecedents\.title": 'The Fireman'}) 

其中沒有工作。

想法?

這實際上干擾了我對一個相當龐大的system.profile集合的探究能力。

在此先感謝。

更新

在迴應評論,我使用的版本是:

$ mongod --version 
db version v2.0.6, pdfile version 4.5 
Sun Oct 14 18:43:29 git version: e1c0cbc25863f6356aa4e31375add7bb49fb05bc 

凱文

+0

您使用的是哪個版本的MongoDB? – Stennie

回答

2

的點查詢的概要文件條目確實出現了一個錯誤MongoDB中的key names並不意味着包括.(或領先的$)。

正如您已經注意到的,這會在嘗試查詢時導致您的問題,因爲dot notation用於指示嵌入對象。

MongoDB中2.2.0有限的解決辦法似乎是使用聚合框架來查詢對象匹配的嵌入文檔:

db.system.profile.aggregate({ $match: {'query': {"antecedents.title" : "The Fireman"}}}) 

我報MongoDB的問題跟蹤爲SERVER-7349此配置錯誤。