如何更改MongoDb模式中的现有字段

如何更改MongoDb模式中的现有字段,mongodb,Mongodb,我有工作基础。如何删除其中一个字段的唯一性属性 connectId:{type:String,index:true,unique:true}, 我需要删除unique:true connectId:{type:String,index:true}, 谢谢您只需使用$unset运算符即可: db.collection.update({},{$unset: {"connectId.unique": ""}}); 请在下面找到有关$unset运算符的更多文档我的解决方案 db.connection

我有工作基础。如何删除其中一个字段的唯一性属性

connectId:{type:String,index:true,unique:true},

我需要删除
unique:true

connectId:{type:String,index:true},


谢谢

您只需使用$unset运算符即可:

db.collection.update({},{$unset: {"connectId.unique": ""}});
请在下面找到有关$unset运算符的更多文档

我的解决方案

db.connections.dropIndex("connectId_1")
db.connections.createIndex({connectId: 1}, {background: "true"})

您必须使用unique:false重新创建索引,谢谢。你能写一个示例代码吗?谢谢!我会试试的,它对我不起作用。WriteResult({“nMatched”:1,“nUpserted”:0,“nModified”:0})