用Mongoose返回更新的收藏 [英] Return updated collection with Mongoose

查看:102
本文介绍了用Mongoose返回更新的收藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用nodejs / express / mongoose / angularjs。我想更新一个名为Lists的集合,它具有多个属性,其中一个是一组项目。在下面的代码中,我在items数组中推送一个新的任务项。一切正常,但是更新功能不会发回更新的集合,那么我必须对数据库执行另一个查询。有没有更有效的方式来做到这一点?



nodejs / express代码

  exports.addTaskToList = function(req,res){
var listId = req.params.Id;
var taskId = req.params.TaskId;
Lists.update({_ id:listId},{$ push:{items:taskId}},{safe:true,upsert:true},function(err,result){
if(err) {
console.log('更新todo列表错误'+ err);
}
else {
console.log(结果+'todo列表条目已更新 - 新增任务已添加');
Lists.findById(listId).populate('items')。exec(function(err,updatedEntry){
if(err){
console.log('Unable to检索todo列表条目');
}
res.send(JSON.stringify(updatedEntry));
});
}
});
};

此外,数组项是ObjectIds的数组。这些项目是单独的模式,因此在单独的集合中。

解决方案

可以推送整个对象,而不仅仅是其_id,以便不会创建另一个集合href =http://mongoosejs.com/docs/documents.html>更新方法 不返回更新的文档:


$ b $但是,如果我们不需要在我们的应用程序中返回的文档,而
只是要直接更新数据库中的一个属性,那么
Model#update是正确的对于我们。


如果您需要更新并返回该文档,请考虑以下选项之一:



传统方法:

  Lists.findById(listId,function(err,list){
if(err){
...
} else {
list.items.push(taskId)
list.save(function(err,list){
...
});
}
});

更简单的方法:

  Lists.findByIdAndUpdate(listId,{$ push:{items:taskId}},function(err,list){
...
}) ;


I work with nodejs/express/mongoose/angularjs. I'd like to update a collection named Lists which has several properties, one of which is an array of items. In the following code, I'm pushing a new task items in the items array. Everything works fine, however the update function does not sends back the updated collection, then I must perform another query on the database. Is there a more efficient way to do this ?

The nodejs/express code :

exports.addTaskToList = function(req, res) {
    var listId = req.params.Id;
    var taskId = req.params.TaskId;
    Lists.update({_id: listId}, {$push: {items: taskId}}, {safe:true, upsert: true}, function(err, result){
        if(err) {
            console.log('Error updating todo list. ' + err);
        }
        else{
            console.log(result + ' todo list entry updated - New task added');
            Lists.findById(listId).populate('items').exec(function (err, updatedEntry) {
                if (err) {
                    console.log('Unable to retrieve todo list entry.');
                }
                res.send(JSON.stringify(updatedEntry));
            });
        }           
    });
};

Furthermore, the array items is an array of ObjectIds. Those items are in a separate schema so in a separate collection. Is it possible to push the whole object and not only its _id so that there is not another collection created ?

解决方案

The update method doesn't return the updated document:

However, if we don't need the document returned in our application and merely want to update a property in the database directly, Model#update is right for us.

If you need to update and return the document, please consider one of the following options:

Traditional approach:

Lists.findById(listId, function(err, list) {
    if (err) {
        ...
    } else {
        list.items.push(taskId)
        list.save(function(err, list) {
            ...
        });
    }
});

Shorter approach:

Lists.findByIdAndUpdate(listId, {$push: {items: taskId}}, function(err, list) {
    ...
});

这篇关于用Mongoose返回更新的收藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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