【问题标题】:MongoDB : Unable to drop a Compound Index on a collectionMongoDB:无法在集合上删除复合索引
【发布时间】:2013-09-10 12:49:53
【问题描述】:

我的收藏中有一个名为 people 的复合索引,如下所示

 db.people.getIndexes()
[
        {
                "name" : "_id_",
                "ns" : "at.people",
                "key" : {
                        "_id" : 1
                }
        },
        {
                "_id" : ObjectId("521dd652a185d3defe301983"),
                "ns" : "at.people",
                "key" : {
                        "personname" : 1,
                        "email" : 1,
                        "sex" : 1,
                        "course" : 1
                },
                "name" : "personname_1_email_1_sex_1_course_1",
                "unique" : false
        }
]

我正在尝试以这种方式删除此索引

 db.people.dropIndex({"personname_1_email_1_sex_1_course_1": 1})

但我收到错误消息

{ "errmsg" : "索引未找到", "ok" : 0 }

我也尝试按名称删除索引

db.people.dropIndex( { "name" : "personname_1_email_1_sex_1_course_1" } )

我知道我可以使用以下命令一次性删除集合中的索引

db.people.dropIndexes()

请告诉我如何解决这个问题?

【问题讨论】:

  • 你应该这样删除它:db.people.dropIndex({personname:1,email:1,sex:1,course:1})
  • 谢谢,上面的命令会删除该字段上的其他单个索引吗?例如,我有一个单独的索引作为 db.people.ensureIndex({"personname" : 1}, {"unique" : false})
  • 不,只会删除那个。

标签: mongodb


【解决方案1】:

将索引名称传递给dropIndex,而不将其放入对象中:

db.people.dropIndex("personname_1_email_1_sex_1_course_1")

【讨论】:

    【解决方案2】:

    我认为你很接近。将您的命令修改为以下命令应该可以工作

    db.people.dropIndex("personname_1_email_1_sex_1_course_1")
    

    【讨论】:

      【解决方案3】:

      只是为了给出完整的答案。

      来自文档:http://docs.mongodb.org/manual/reference/method/db.collection.dropIndex/ 你应该使用 db.collection.dropIndex()string or document

      指定要删除的索引。您可以通过以下方式指定索引 索引名称或索引规范文档。

      两者兼有:

      db.people.dropIndex("personname_1_email_1_sex_1_course_1")
      db.people.dropIndex({personname:1,email:1,sex:1,course:1})
      

      工作正常。

      【讨论】:

        【解决方案4】:

        删除 mongodb 索引

            Syntax :  db.collection.dropIndex(index)
        

        这里,

        索引:类型:字符串或文档

        指定要删除的索引。您可以通过以下方式指定索引 索引名称或由索引规范文档来删除文本 index,指定索引名称为字符串类型

        在你的情况下:

        db.people.dropIndex("personname_1_email_1_sex_1_course_1")
        

        详细信息如下: 考虑一个宠物收藏。在 pets 集合上调用 getIndexes() 方法会返回以下索引:

        [
           {  "v" : 1,
              "key" : { "_id" : 1 },
              "ns" : "test.pets",
              "name" : "_id_"
           },
           {
              "v" : 1,
              "key" : { "cat" : -1 },
              "ns" : "test.pets",
              "name" : "catIdx"
           },
           {
              "v" : 1,
              "key" : { "cat" : 1, "dog" : -1 },
              "ns" : "test.pets",
              "name" : "cat_1_dog_-1"
           }
        ]
        

        字段cat上的单字段索引有用户指定名称catIdx和索引规范文档{ "cat" : -1 }。

        要删除索引 catIdx,您可以使用索引名称:

        db.pets.dropIndex( "catIdx" )
        

        或者你可以使用索引规范文档 { "cat" : -1 }:

        db.pets.dropIndex( { "cat" : -1 } )
        

        参考:https://docs.mongodb.com/manual/reference/method/db.collection.dropIndex/#index-name

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-11-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-05
          • 1970-01-01
          • 2012-01-20
          • 1970-01-01
          相关资源
          最近更新 更多