如何使用mongoose从集合的所有文档中删除指定的字段?

时间:2017-04-13 11:45:28

标签: node.js mongodb mongoose

我想使用mongoose从用户集合中删除所有文档中的密钥“passwrod”,是否可以使用$ unset进行删除?

 { "_id" : ObjectId("58ec890c91b2b612084fd827"),
        "username" : "zain",
        "passwrod" : 123,
        "password" : 8 },
{   "_id" : ObjectId("58ec8918364116187845948d"),
        "username" : "bob",
        "password" : 123,
        "passwrod" : 12  }

1 个答案:

答案 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 }