在MongoDB中更新具有不同值的记录 [英] Update records with different values in MongoDB

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

问题描述

我试图为自己节省很多查询,我将Mongoose用作ODM.

I'm trying to save myself a lot of queries, I'm using Mongoose as ODM.

我有一个相当大的数组topic_codes,例如50k +个元素,我正在这样做(CoffeeScript):

I have a rather large array topic_codes, say 50k+ elements, I'm doing this (CoffeeScript):

  conditions = code: $in: topic_codes
  update     = $push: samples: date: point_in_time, volume: ??
  options    = multi: true
  TopicArchive.update conditions, update, options, (err) ->

在这里,我试图在文档的 samples 中插入一个新的子文档,该文档是一个数组,其对象带有两个属性,即 date 音量.

Here I am trying to insert a new subdocument in 'samples', which is an array, of my document with an object with two attributes, date and volume.

虽然日期与我要更新的每条记录相同,但音量却不一样,并且每条记录都会有所不同.

While date is the same for every record I'd like to update, volume it's not and can vary from record to record.

有没有一种方法可以实现我的目标而不必经历巨大的数据库攻击?

Is there a way to achieve my goal without having to go through a huge database hit?

推荐答案

多重更新会根据更新说明符更新所有匹配的文档.如果要使用不同的值更新每个文档,则必须为N个文档发布N个更新(或更确切地说,为将要使用的N个更新说明符中的每一个发布N个更新).例如,如果volumetopic_codes少得多,则可以发出一系列多次更新,其中每次更新仅触及应该具有相同volume的那些文档,并使用$in是.

A multi update updates all matching documents according to the update specifier. If you want to update each document with a different value, then you must issue N updates for N documents (or, more precisely, N updates for each of the N update specifiers you will use). For example, if you have many fewer volumes than topic_codes, you can issue a series of multi-updates, where each update only touches those documents which should have the same volume, using $in as you already are.

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

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