开发者社区> 问答> 正文

MongoDB内嵌文档的索引无效:配置报错 

正在学习MongoDB到“内嵌索引”部分,动手过程如下:

peoples = [
{"name":{"first":"joe", "last":"schome"}, "age":27},
{"name":{"first":"simon", "last":"wade"}, "age":18},
{"name":{"first":"joe", "last":"kelly"}, "age":30}
]
成功插入集合:
> db.peoples.insert(peoples)
BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 3,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]
})
成功创建索引:
> db.peoples.ensureIndex({"name.first":1, "age":-1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> db.peoples.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.peoples"
        },
        {
                "v" : 1,
                "key" : {
                        "name.first" : 1,
                        "age" : -1
                },
                "name" : "name.first_1_age_-1",
                "ns" : "test.peoples"
        }
]
查询:
> db.peoples.find().pretty()
{
        "_id" : ObjectId("556dcd540d56f64140620b54"),
        "name" : {
                "first" : "joe",
                "last" : "schome"
        },
        "age" : 27
}
{
        "_id" : ObjectId("556dcd540d56f64140620b55"),
        "name" : {
                "first" : "simon",
                "last" : "wade"
        },
        "age" : 18
}
{
        "_id" : ObjectId("556dcd540d56f64140620b56"),
        "name" : {
                "first" : "joe",
                "last" : "kelly"
        },
        "age" : 30
}
>
为什么创建的索引没有发生作用呢?

于是,尝试删除上面创建的索引,对age创建一个简单的索引:

> db.peoples.dropIndex("name.first_1_age_-1")
{ "nIndexesWas" : 2, "ok" : 1 }
> db.peoples.ensureIndex({"age":-1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> db.peoples.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.peoples"
        },
        {
                "v" : 1,
                "key" : {
                        "age" : -1
                },
                "name" : "age_-1",
                "ns" : "test.peoples"
        }
]

> db.peoples.find().pretty()
{
        "_id" : ObjectId("556dcd540d56f64140620b54"),
        "name" : {
                "first" : "joe",
                "last" : "schome"
        },
        "age" : 27
}
{
        "_id" : ObjectId("556dcd540d56f64140620b55"),
        "name" : {
                "first" : "simon",
                "last" : "wade"
        },
        "age" : 18
}
{
        "_id" : ObjectId("556dcd540d56f64140620b56"),
        "name" : {
                "first" : "joe",
                "last" : "kelly"
        },
        "age" : 30
}
非内嵌文档索引一样没效果……

真是百思不得其解~

展开
收起
kun坤 2020-06-02 19:27:48 899 0
1 条回答
写回答
取消 提交回答
  • 你的问题让我好奇怪,你这个find()就是查出所有的数据了,里面又没有任何条件,索引怎么可能起作用呢……而且,你紧紧通过 db.peoples.find().pretty()就想看索引有无起作用是不对的,应该是db.peoples.find().explain(),通过这个explain()来看看mongodb有无使用到索引的######

    引用来自“jakic”的评论

    你的问题让我好奇怪,你这个find()就是查出所有的数据了,里面又没有任何条件,索引怎么可能起作用呢……而且,你紧紧通过 db.peoples.find().pretty()就想看索引有无起作用是不对的,应该是db.peoples.find().explain(),通过这个explain()来看看mongodb有无使用到索引的 多谢指教~
    2020-06-02 19:28:00
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
开源数据库 MongoDB 专场 MongoDB疑难杂症分析及优化 立即下载
阿里云MongoDB云服务构建 立即下载
饿了么高级架构师陈东明:MongoDB是如何逐步提高可靠性的 立即下载