使用不同的值更新多个文档 [英] Update multiple docs with different values

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

问题描述

完全不可能,但想检查一下.

Don't think this is at all possible but wanted to check.

如果我有以下文件:

doc1 : {
  _id: 1,
  name: 'John Smith',
  age: 20
}

doc2 : {
  _id: 2,
  name: 'Jane Smith',
  age: 22
}

我希望客户端能够在相同的更新请求中将两个文档传递给我.在这种情况下,可以在两个文档中都添加一个地址.

I want the client to be able to pass me both docs in the same request for update. In this case maybe add an address to both docs.

有没有一种方法可以向mongo发送一条更新语句,以便它使用名称值更新两个文档?

Is there a way the I can send one update statement to mongo such that it updates both of the documents with the name values?

I.E.来自客户:

doc1 : {
  _id: 1,
  name: 'John Smith',
  age: 20,
  address: '123 Street'
}

doc2 : {
  _id: 2,
  name: 'Jane Smith',
  age: 22,
  address: '456 Way'
}

目前,我正在遍历这些值并一次更新一个.问题是猫鼬/mongodb更新是异步的,因此我无法可靠地告诉客户端我更新了每个结果,直到所有更新回调均已触发.我有一个计数器来确保收到N个回调,然后发送响应.

Currently I am iterating over the values and updating one at a time. Problem with that is mongoose/mongodb updates are async, so I cannot reliably tell the client that I updated each result until all update callbacks have fired. I have a counter to make sure I receive N number of callbacks then I send a response.

推荐答案

我所做的所有更新都是一次更新,但是对我而言,好处是它们是同步的. =)

All the updates I do are one at a time, but what's nice for me is that they are synchronous. =)

似乎可以使用mongoimport工具批量加载: http://docs.mongodb.org/manual/reference/mongoimport/#cmdoption-mongoimport--upsertFields .您可以通过这种方式进行批量增补.

It seems you can bulk load though with the mongoimport tool: http://docs.mongodb.org/manual/reference/mongoimport/#cmdoption-mongoimport--upsertFields. You can do bulk upserts that way.

否则,我不相信您可以通过客户端在一个命令中完成该操作.

Otherwise I don't believe you can do it in one command via a client.

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

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