Mongoose查找和删除 [英] Mongoose Find and Remove

查看:2588
本文介绍了Mongoose查找和删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除多个满足查询的文档。然而,我需要这些文档的数据,以将它们存储在单独的集合中,用于撤消功能。我得到这个工作的唯一方法是多个查询:

  Data.find(query).exec数据)
{
Data.remove(query).exec(function(err2)
{
ActionCtrl.saveRemove(data);
});
});

有更好的方法吗?在此信息中:如何使用Node.js删除文档Mongoose ?建议使用find()。remove()。exec():

  Data.find查询).remove()。exec(function(err,data)
{
ActionCtrl.saveRemove(data);
});

data 通常 1 ,不要问我为什么。我可以这样做,无需无限嵌套我的查询?谢谢!

解决方案

正如您所注意到的,使用以下操作不会返回文档:

  Data.find(query).remove()。exec(function(err,data){
// data will equals the document of documentss removed,不是文档本身
}

因此, code> ActionCtrl 使用此方法。



您可以使用原始方法或使用某种形式的迭代实现相同的结果。像异步这样的控制流库可能派上用场来处理异步调用,它不会减少您的代码,但会减少查询。请参阅示例:

  Data.find(query,function(err,data){
async.each(data,function(dataItem,callback){
dataItem.remove(function(err,result){
ActionCtrl.saveRemove(result,callback);
});
});
});

$ c> ActionCtrl.saveRemove()实现可以将单个文档作为参数,并且可以从 async.each 循环执行回调。 async.each 需要在每次迭代结束时运行没有参数的回调,因此您最好在 .saveRemove()


结束时运行此注意注意 remove 方法将实际返回已删除的文档。


I'm trying to delete multiple documents that satisfy a query. However I need the data of those documents for storing them in a separate collection for undo functionality. The only way I got this to work is with multiple queries:

Data.find(query).exec(function(err, data)
{
    Data.remove(query).exec(function(err2)
    {
        ActionCtrl.saveRemove(data);
    });
});

Is there a better way? In this post: How do I remove documents using Node.js Mongoose? it was suggested to use find().remove().exec():

Data.find(query).remove().exec(function(err, data)
{
    ActionCtrl.saveRemove(data);
});

However data is usually 1, don't ask me why. Can I do this without infinitely nesting my queries? Thanks!

解决方案

As you have noted, using the following will not return the document:

Data.find(query).remove().exec(function(err, data) {
  // data will equal the number of docs removed, not the document itself
}

As such, you can't save the document in ActionCtrl using this approach.

You can achieve the same result using your original approach, or use some form of iteration. A control flow library like async might come in handy to handle the async calls. It won't reduce your code, but will reduce the queries. See example:

Data.find(query, function(err, data) {
  async.each(data, function(dataItem, callback) {
    dataItem.remove(function(err, result) {
      ActionCtrl.saveRemove(result, callback);
    });
  });
});

This answer assumes that the ActionCtrl.saveRemove() implementation can take an individual doc as a parameter, and can execute the callback from the async.each loop. async.each requires a callback to be run without arguments at the end of each iteration, so you would ideally run this at the end of .saveRemove()

Note that the remove method on an individual document will actually return the document that has been removed.

这篇关于Mongoose查找和删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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