如何使用Node.js Mongoose删除文档? [英] How do I remove documents using Node.js Mongoose?

查看:198
本文介绍了如何使用Node.js Mongoose删除文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FBFriendModel.find({
    id: 333
}, function (err, docs) {
    docs.remove(); //Remove all the documents that match!
});

上面似乎不工作。记录仍然存在。

The above doesn't seem to work. The records are still there.

有人可以修复吗?

推荐答案

docs 是一个文档数组。因此它没有 mongooseModel.remove()方法。

docs is an array of documents. so it doesn't have a mongooseModel.remove() method.

您可以迭代和删除每个文档

You can iterate and remove each document in the array separately.

或者 - 因为它看起来像是通过一个(可能)唯一的id查找文档 - 使用 findOne 而不是 find

Or - since it looks like you are finding the documents by a (probably) unique id - use findOne instead of find.

这篇关于如何使用Node.js Mongoose删除文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆