2011-03-04 64 views
2
var jd = { 
    type: "Person", 
    attributes: { 
    name: "John Doe", 
    age: 30 
    } 
}; 

var pd = { 
    type: "Person", 
    attributes: { 
    name: "Penelope Doe", 
    age: 26 
    } 
}; 

var ss = { 
    type: "Book", 
    attributes: { 
    name: "The Sword Of Shannara", 
    author: "Terry Brooks" 
    } 
}; 

db.things.save(jd); 
db.things.save(pd); 
db.things.save(ss); 
db.things.ensureIndex({attributes: 1}) 
db.things.find({"attributes.age": 30}) // => John Doe 
db.things.find({"attributes.age": 30}).explain() // => BasicCursor... (don't want a scan) 
db.things.find({"attributes.age": {$gte: 18}) // John Doe, Penelope Doe (via a scan) 

目標是通過範圍查詢對所有屬性進行索引和搜索,並且實際使用索引(而不是集合掃描)。沒有說明文件具有什麼屬性。我已閱讀關於多鍵的內容,但他們似乎只能用精確匹配查詢工作(通過索引)。MongoDB:使用多鍵可以進行範圍查詢嗎?

Multikeys喜歡這種格式的文檔:

var pd = { 
    type: "Person", 
    attributes: [ 
    {name: "Penelope Doe"}, 
    {age: 26} 
    ] 
}; 

有一個指數在哪裏可以找到使用範圍內的屬性項目的模式?

編輯:

在一個無模式DB是有意義的有潛在的類型無限陣列,但一個集合名稱實際上暗示着某種類型的。但是,如果我們走向極端,我們想要在一個集合中允許任意數量的類型(這樣我們就不必爲用戶可能想象到的每個可以想到的自定義類型定義一個集合)。因此,使用只有一個深度索引(支持範圍查詢)的屬性(任何類型)搜索,使得這種事情更加可行。似乎對我來說天生適合無模式數據庫。

開了票,如果你想投上一票:

http://jira.mongodb.org/browse/SERVER-2675

+1

我會在http://groups.google.com/group/mongodb-user中發佈這個問題,並從開發者自己那裏得到答案 - 他們*非常*響應。 – 2011-03-04 01:33:20

+0

感謝您的提示。我做到了。 :) – Mario 2011-03-04 03:16:11

回答

-3

範圍查詢也是可能的使用multikeys;然而,表達查詢可能會很棘手。

0

是範圍查詢multikeys工作。但是多鍵是用於數組而不是嵌入對象。

在上面的例子嘗試

db.things.ensureIndex({"attributes.age": 1}) 
+0

目標是避免索引「年齡」或任何一個屬性,但要索引所有「屬性」。關鍵是我可以有任何屬性鍵(例如「dob」,「name」,「hair_color」),我應該索引訪問它們。 – Mario 2011-03-05 00:07:48