通过提供正文mongoose/mongodb中的文档来更新多个文档

编程入门 行业动态 更新时间:2024-10-23 12:26:28
本文介绍了通过提供正文mongoose/mongodb中的文档来更新多个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要通过在正文中提供几个文件来更新它们.我无法查询它们,必须提供它们.

I need to update several documents by providing them in the body. I can't query them, they must be provided.

示例:

var persons = [ {id: 1, name'Joe', active: false}, {id:2, name:'Jane', active: false}) ];

此数据在正文中提供,我想将active属性设置为false.

This data is provided in the body and I want to set the active property to false.

exports.setActivePropertyOnPersons = function(input,callback){ for(var i = 0;i<input.body.length;i++){ mongoose.model('person').findOne({id:input.body[i].id}, function(err, person){ person.active = false; person.save(); }) } callback.send(200) };

这段代码感觉不好.有没有更好的查询来做到这一点?我在文档中找不到任何内容.

This code feels no good. Is there any better query to do this? I don't find any in the docs.

推荐答案

尝试将更新命令与" $ in "运算符:

Try using the update command along with the "$in" operator:

var ids= []; for (var i=0 i<input.body.length; ++i) { ids.push(input.body[i].id); } mongoose.model('person').update( {id : {"$in":ids}}, {active:false} , {multi: true} , function(err,docs) { ... });

希望这会有所帮助

更多推荐

通过提供正文mongoose/mongodb中的文档来更新多个文档

本文发布于:2023-11-22 18:37:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618546.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   多个   正文   mongodb   mongoose

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!