【问题标题】:Remove single field from index mongod [duplicate]从索引 mongod 中删除单个字段 [重复]
【发布时间】:2019-07-09 09:56:26
【问题描述】:

我无法从索引中删除单个字段

db.customer.getIndexes()

{
    "v" : 2,
    "unique" : true,
    "key" : {
        "customer_id" : 1
    },
    "name" : "customer_id_1",
    "background" : true,
    "ns" : "clarks-flo-live2.customer"
}

我正在尝试从上面删除 "unique" : true,,请帮帮我。

【问题讨论】:

    标签: database mongodb mongoose mongodb-query


    【解决方案1】:
       db.customer.update(
           {"name" : "customer_id_1" },
           { $unset: { "unique" : true } }
        )
    

    $unset 表达式中的指定值(即“”)不影响操作。

    要在嵌入文档或数组中指定 a,请使用点表示法。

    行为 如果该字段不存在,则 $unset 什么都不做(即不做任何操作)。

    当与 $ 一起使用以匹配数组元素时,$unset 将匹配元素替换为 null,而不是从数组中删除匹配元素。这种行为使数组大小和元素位置保持一致。

    【讨论】:

    • 嗨,我试过了,但没有运气,db.customer.update( {"name" : "customer_id_1" }, { $unset: { "unique" : true } } ); WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
    • { $unset: { "unique" : true } } ,{multi :true} 或 { $unset: { "unique" : 1 } } ,{multi :true} --投票给我
    • 这将修改匹配的文档,而不是像 OP 需要的索引。
    • -- ### 点击链接 (docs.mongodb.com/manual/reference/method/…)。 ### :- 从集合中删除或删除指定的索引。 db.collection.dropIndex() 方法为 dropIndexes 命令提供了一个包装器。您不能删除 _id 字段上的默认索引。要删除文本索引,请指定索引名称。
    猜你喜欢
    • 2012-03-13
    • 2013-10-27
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    相关资源
    最近更新 更多