我试图弄清楚我必须如何构建查询,以便它们将达到我的索引.我的文档结构如下:
{ "attributes" : { "make" : "Subaru", "color" : "Red" } }
Run Code Online (Sandbox Code Playgroud)
索引为: db.stuff.ensureIndex({"attributes.make":1})
我发现使用点表示法查询命中索引,而查询文档却没有.
例:
db.stuff.find({"attributes.make":"Subaru"}).explain()
{
"cursor" : "BtreeCursor attributes.make_1",
"nscanned" : 2,
"nscannedObjects" : 2,
"n" : 2,
"millis" : 0,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
"attributes.make" : [
[
"Subaru",
"Subaru"
]
]
}
}
Run Code Online (Sandbox Code Playgroud)
VS
db.stuff.find({attributes:{make:"Subaru"}}).explain()
{
"cursor" : "BasicCursor",
"nscanned" : 2,
"nscannedObjects" : 2,
"n" : 0,
"millis" : 1,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让文档样式查询命中索引?原因是当从我的持久对象构造查询时,将它们作为文档序列化比使用点表示法更容易.
我还要补充一点,我们正在使用一个由Jackson构建的本土数据映射器层.使用像Morphia这样的东西有助于正确构建这些查询吗?
做了一些挖掘,这个帖子解释了子文档查询的内容.我上面的问题是,使基于子文档的查询的行为类似于我需要使用elemMatch的点符号.
db.stuff.find({"attributes":{"$elemMatch" : {"make":"Subaru"}}}).explain()
{
"cursor" : "BtreeCursor attributes.make_1",
"nscanned" : 2,
"nscannedObjects" : 2,
"n" : 0,
"millis" : 2,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
"attributes.make" : [
[
"Subaru",
"Subaru"
]
]
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3061 次 |
| 最近记录: |