12

我正在为我们的客户评估 MongoDB。根据要求,我们需要与一些ent名称-值对的实体变量集相关联。

db.ent.insert({'a':5775, 'b':'b1'})
db.ent.insert({'c':'its a c', 'b':'b2'})
db.ent.insert({'a':7557, 'c':'its a c'})

在此之后,我需要集中查询ent字段的存在:

db.ent.find({'a':{$exists:true}})
db.ent.find({'c':{$exists:false}})

根据 MongoDB文档

$exists 即使使用索引也不是很有效,尤其是。使用 {$exists:true} 因为它必须有效地扫描所有索引值。

那里的专家能否提供更有效的方法(即使改变范式)来快速处理不同的名称-值对

4

4 回答 4

9

您可以像这样重新设计架构:

{
  pairs:[
  {k: "a", v: 5775},
  {k: "b", v: "b1"},
  ]
}

然后你索引你的键:

db.people.ensureIndex({"pairs.k" : 1})

在此之后,您将能够按完全匹配进行搜索:

db.ent.find({'pairs.k':"a"})

如果您使用由@WesFreeman 提出的稀疏索引和当前模式,您将需要在要搜索的每个键上创建一个索引。如果您的密钥不是静态的,它可能会影响写入性能,或者是不可接受的。

于 2012-01-25T21:24:00.087 回答
2

只需重新设计您的架构,使其成为可索引的查询。您的用例实际上类似于MongoDB The Definitive Guide中给出的第一个示例应用程序。

如果您想要/需要方便地result.a将密钥存储在可索引的地方。

而不是现有的:

db.ent.insert({a:5775, b:'b1'})

db.ent.insert({a:5775, b:'b1', index: ['a', 'b']})

那是一个可索引的查询:

db.end.find({index: "a"}).explain()
{
    "cursor" : "BtreeCursor index_1",
    "nscanned" : 1,
    "nscannedObjects" : 1,
    "n" : 1,
    "millis" : 0,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "isMultiKey" : true,
    "indexOnly" : false,
    "indexBounds" : {
        "index" : [
            [
                "a",
                "a"
            ]
        ]
    }
}

或者如果您也可能按值查询:

db.ent.insert({
    a:5775, 
    b:'b1', 
    index: [
        {name: 'a', value: 5775}, 
        {name: 'b', value: 'b1'}
    ]
})

这也是一个可索引的查询:

db.end.find({"index.name": "a"}).explain()
{
    "cursor" : "BtreeCursor index.name_",
    "nscanned" : 1,
    "nscannedObjects" : 1,
    "n" : 1,
    "millis" : 0,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "isMultiKey" : true,
    "indexOnly" : false,
    "indexBounds" : {
        "index.name" : [
            [
                "a",
                "a"
            ]
        ]
    }
}
于 2012-01-26T09:23:08.513 回答
1

我认为稀疏索引是解决这个问题的答案,尽管您需要为每个字段创建一个索引。http://www.mongodb.org/display/DOCS/Indexes#Indexes-SparseIndexes

稀疏索引应该有助于 $exists:true 查询。

即使如此,如果您的字段不是真的稀疏(意味着它大部分是固定的),它也不会对您有太大帮助。

更新我想我错了。看起来有一个未解决的问题(https://jira.mongodb.org/browse/SERVER-4187)仍然 $exists 不使用稀疏索引。但是,您可以使用 find 和 sort 执行类似的操作,看起来它正确使用了稀疏索引:

db.ent.find({}).sort({a:1});

这是使用您的示例值的差异的完整演示:

> db.ent.insert({'a':5775, 'b':'b1'})
> db.ent.insert({'c':'its a c', 'b':'b2'})
> db.ent.insert({'a':7557, 'c':'its a c'})
> db.ent.ensureIndex({a:1},{sparse:true});

注意find({}).sort({a:1})使用索引(BtreeCursor):

> db.ent.find({}).sort({a:1}).explain();
{
"cursor" : "BtreeCursor a_1",
"nscanned" : 2,
"nscannedObjects" : 2,
"n" : 2,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
    "a" : [
        [
            {
                "$minElement" : 1
            },
            {
                "$maxElement" : 1
            }
        ]
    ]
}
}

find({a:{$exists:true}})进行全面扫描:

> db.ent.find({a:{$exists:true}}).explain();
{
"cursor" : "BasicCursor",
"nscanned" : 3,
"nscannedObjects" : 3,
"n" : 2,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {

}
}

看起来您也可以使用 .hint({a:1}) 强制它使用索引。

> db.ent.find().hint({a:1}).explain();
{
"cursor" : "BtreeCursor a_1",
"nscanned" : 2,
"nscannedObjects" : 2,
"n" : 2,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
    "a" : [
        [
            {
                "$minElement" : 1
            },
            {
                "$maxElement" : 1
            }
        ]
    ]
}
}
于 2012-01-25T21:02:57.007 回答
0

如何将不存在的字段设置为null?然后您可以使用 查询它们{field: {$ne: null}}

db.ent.insert({'a':5775, 'b':'b1', 'c': null})
db.ent.insert({'a': null, 'b':'b2', 'c':'its a c'})
db.ent.insert({'a':7557, 'b': null, 'c':'its a c'})

db.ent.ensureIndex({"a" : 1})
db.ent.ensureIndex({"b" : 1})
db.ent.ensureIndex({"c" : 1})

db.ent.find({'a':{$ne: null}}).explain()

这是输出:

{
    "cursor" : "BtreeCursor a_1 multi",
    "isMultiKey" : false,
    "n" : 4,
    "nscannedObjects" : 4,
    "nscanned" : 5,
    "nscannedObjectsAllPlans" : 4,
    "nscannedAllPlans" : 5,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "indexBounds" : {
        "a" : [
            [
                {
                    "$minElement" : 1
                },
                null
            ],
            [
                null,
                {
                    "$maxElement" : 1
                }
            ]
        ]
    },
    "server" : "my-laptop"
}
于 2013-07-03T09:24:27.723 回答