我想使用mongoose从用户集合中删除所有文档中的密钥“passwrod”,是否可以使用$ unset进行删除?
{ "_id" : ObjectId("58ec890c91b2b612084fd827"),
"username" : "zain",
"passwrod" : 123,
"password" : 8 },
{ "_id" : ObjectId("58ec8918364116187845948d"),
"username" : "bob",
"password" : 123,
"passwrod" : 12 }
答案 0 :(得分:2)
文件:
{ "_id" : ObjectId("58ec890c91b2b612084fd827"), "username" : "zain", "passwrod" : 123, "password" : 8 }
{ "_id" : ObjectId("58ec8918364116187845948d"), "username" : "bob", "password" : 123, "passwrod" : 12 }
查询:
db.collection.updateMany({}, {$unset:{"passwrod":1}})
结果:
{ "_id" : ObjectId("58ec890c91b2b612084fd827"), "username" : "zain", "password" : 8 }
{ "_id" : ObjectId("58ec8918364116187845948d"), "username" : "bob", "password" : 123 }