2011-03-04 70 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