且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Mongoose从MongoDB文档中删除密钥

更新时间:2023-01-09 09:08:54

在早期版本中,您需要删除node-mongodb-native驱动程序.每个模型都有一个收集对象,该对象包含node-mongodb-native提供的所有方法.因此,您可以通过以下方式执行相关操作:

In early versions, you would have needed to drop down the node-mongodb-native driver. Each model has a collection object that contains all the methods that node-mongodb-native offers. So you can do the action in question by this:

User.collection.update({_id: user._id}, {$unset: {field: 1 }});

从2.0版开始,您可以执行以下操作:

Since version 2.0 you can do:

User.update({_id: user._id}, {$unset: {field: 1 }}, callback);

从2.4版开始,如果您已经有模型的实例,则可以执行以下操作:

And since version 2.4, if you have an instance of a model already you can do:

doc.field = undefined;
doc.save(callback);