MongoDB updateMany 带条件 [英] MongoDB updateMany with Conditional

查看:85
本文介绍了MongoDB updateMany 带条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有各种不一致的大型数据库.我想清除的项目之一是根据人口更改国家/地区状态.

I have a large DB with various inconsistencies. One of the items I would like to clear up is changing the country status based on the population.

数据样本是:

{ "_id" : "D", "name" : "Deutschland", "pop" : 70000000, "country" : "Large Western" }
{ "_id" : "E", "name" : "Eire", "pop" : 4500000, "country" : "Small Western" }
{ "_id" : "G", "name" : "Greenland", "pop" : 30000, "country" : "Dependency" }
{ "_id" : "M", "name" : "Mauritius", "pop" : 1200000, "country" : "Small island"}
{ "_id" : "L", "name" : "Luxembourg", "pop" : 500000, "country" : "Small Principality" }

显然,我想根据人口规模将国家/地区字段更改为更统一的内容.

Obviously I would like to change the country field go something more uniform, based on population size.

我已经尝试过这种方法,但显然缺少某种与国家/地区字段更新相关联的方法.

I've tried this approach, but obviously missing some way of tying into an update of the country field.

db.country.updateMany( { case : { $lt : ["$pop" : 20000000] }, then : "Small country" }, { case : { $gte : ["$pop" : 20000000] }, then : "Large country" }

在我写完之前发布.

我正在考虑使用 $cond 功能,在使用 updateMany 时基本上返回如果为真,则执行 X,如果为假,则执行 y.

I was thinking to use $cond functionality, to basically return if true, do X, if false, do y, while using the updateMany.

这是可能的,还是有解决方法?

Is this possible, or is there a workaround?

推荐答案

你真的想要 bulkWrite() 在其中使用两个 "updateMany" 语句代替.聚合表达式不能用于任何形式的更新语句中的交替选择".

You really want want bulkWrite() using two "updateMany" statements within it instead. Aggregation expressions cannot be used to do "alternate selection" in any form of update statement.

db.country.bulkWrite([
  { "updateMany": {
    "filter": { "pop": { "$lt": 20000000 } },
    "update": { "$set": { "country": "Small Country" } }
  }},
  { "updateMany": {
    "filter": { "pop": { "$gt": 20000000 } },
    "update": { "$set": { "country": "Large Country" } } 
  }}
])

SERVER-6566 上仍有一个未完成的功能请求",用于条件语法",但这尚未解决.批量"API 实际上是在提出此请求后引入的,并且确实可以调整,如图所示或多或少做同样的事情.

There is still an outstanding "feature request" on SERVER-6566 for "conditional syntax", but this is not yet resolved. The "bulk" API was actually introduced after this request was raised, and really can be adapted as shown to do more or less the same thing.

也使用 $out 在聚合语句中,否则建议不是 更新" 的选项,目前只能写入新集合".从 MongoDB 4.2 开始的预定更改将允许 $out 实际上更新"一个现有的集合,但是这只会是要更新的集合与任何集合不同的地方在从聚合管道收集数据时使用的其他集合.因此,不可能使用聚合管道来更新相同集合作为您正在阅读的内容.

Also using $out in an aggregation statement as was otherwise suggested is not an option to "update" and can only write to a "new collection" at present. The slated change from MongoDB 4.2 onwards would allow $out to actually "update" an existing collection, however this would only be where the collection to be updated is different from any other collection used within the gathering of data from the aggregation pipeline. So it is not possible to use an aggregation pipeline to update the same collection as what you are reading from.

简而言之,使用bulkWrite().

In short, use bulkWrite().

这篇关于MongoDB updateMany 带条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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