在猫鼬中更新具有不同值的多个元素 [英] Update multiple elements with different value in Mongoose

查看:46
本文介绍了在猫鼬中更新具有不同值的多个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含列表的文档.可以说是:

I have document containing lists. Lets say they are:

[
 {
  _id: 52b37432b2395e1807000008,
  name: ListA,
  order: 1,
  desc: 'More about List A'
 },
 {
  _id: 52b37432b2395e1807000009,
  name: LISTB,
  order: 2,
  desc: 'More about List B'
 },
 {
  _id: 52b37432b2395e180700000e,
  name: LISTC,
  order: 3,
  desc: 'More about List C'
 },
 {
  ..
 } 
]

现在,我想使用批量更新来更改其订单字段.我有一个update_stage订单的JSON

Now I want to change their order field using a batch update. I have a JSON of updated_stage order

var updated_stage = [{_id: '52b37432b2395e1807000008', order:2},{_id: '52b37432b2395e180700000e', order:1}, {_id: '52b37432b2395e1807000009', order:3}]

现在我需要用我拥有的新数组更新Mongoose中的LIST模型.我知道我可以使用批量更新来更新具有相同值的多个文档

Now I need to update LIST Model in Mongoose with the new array that I have. I know that I can update multiple documents with same value using Batch Update

Model.update({ }, { $set: { order: 10 }}, { multi: true }, callback);

但是我必须用不同的值来更新它们.我该怎么办?什么是最有效的方法?

But I have to update them by different values. How should I do it? Whats the most efficient way?

推荐答案

我想到的最有效的方法是在updated_satge数组上运行forEach循环.现在,在mongodb的现有文档中采用_id并更新顺序.

The most efficient way I could think of is to run a forEach loop over your updated_satge array. Now take the _id and update order in the existing document in mongodb.

这篇关于在猫鼬中更新具有不同值的多个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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