更新具有不同值的多个文档的方法 [英] way to update multiple documents with different values

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

问题描述

我有以下文件:

[{
  "_id":1,
  "name":"john",
  "position":1
},
 {"_id":2,
  "name":"bob",
  "position":2
},
 {"_id":3,
  "name":"tom",
  "position":3
}]

在UI中,用户可以更改项目的位置(例如,将Bob移到第一位置,john移到位置2,tom移到位置3). 有没有办法一次更新所有文档中的所有职位?

In the UI a user can change position of items(eg moving Bob to first position, john gets position 2, tom - position 3). Is there any way to update all positions in all documents at once?

推荐答案

您不能使用MongoDB查询一次更新两个文档.您将始终必须在两个查询中执行此操作.您当然可以将字段的值设置为相同的值,或者以相同的数字递增,但是您不能在MongoDB中使用相同的查询进行两次不同的更新.

You can not update two documents at once with a MongoDB query. You will always have to do that in two queries. You can of course set a value of a field to the same value, or increment with the same number, but you can not do two distinct updates in MongoDB with the same query.

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

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