使用 upsert 和多语法更新 Mongodb [英] Mongodb update with upsert and multi syntax

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

问题描述

我是 mongodb 的新手,因为 mongodb 的不完整文档让我反复试验而感到压力很大……遗憾的是,我的所有尝试都没有成功,这让我对正在发生的事情和要调试的内容感到困惑...

I am new to mongodb and so stressed out because of mongodb's incomplete documentation leaving me for trial and error... sadly, all my attempts are not working with no error, leaving me confused about what was happening and what to debug...

我只需要更新数据库上匹配特定条件的多条记录,对于不存在的记录,为此创建新条目.我相信我可以通过更新、upsert 和 multi 的单一数据库访问来做到这一点.这是我想出的:

I just need to update multiple records on the database matching certain criteria, and for the non existing records, create new entries for that. I believe I can do it with a single database access with update, upsert and multi. Here's what I've come up with:

dbschema.Person.update( { person_id: { $in: ["734533604" ,"701084015"] } }, { $set: {"scores": 1200} }, { options: { upsert: true, multi: true } } );

我也试过多种组合甚至旧版本如:

I've also tried multiple combinations or even the old version such as:

dbschema.Person.update( { person_id: { $in: ["734533604" ,"701084015"] } }, { $set: {"scores": 1200} }, { upsert: true }, { multi: true } );

它们都不起作用...

请帮我处理这些琐碎的事情...我可以在 sql 中轻松完成,但是 nosql 的东西对我来说太有限了..谢谢!

Please help me with this so trivial stuff... I can easily do it in sql, but the nosql thingy is so limiting on me.. Thanks!

对 find 的相同查询非常有效:

The same query on find works perfectly:

dbschema.Person.find( { person_id: { $in: ["734533604" ,"701084015"] } }, function ( err, results ) {
    console.log( 'result: ' + results );
    console.log( 'error: ' + err );
    console.log( 'result length: ' + results.length );
} );

我期待创建未找到"记录,并更新找到的记录.我的逻辑可能有缺陷,我现在很困惑.

I am expecting the "not found" record to be created, and the found record to be updated. my logic may be flawed and I am so much confused now.

本来,我是一次find()-ing一条记录,改变值,并为每条修改的记录调用save(),但是当我部署到live时,响应时间特别慢了数百倍当每个请求有几百条记录需要更新时.

Originally, I was find()-ing one record at a time, change the value, and called save() for each of the record modified, but when I deployed to live, the response time become hundreds of time slower especially when there's a few hundred records to be updated on each request.

然后我找到了find()+$in,性能恢复了,甚至比以前更好(查询时),但更新仍然慢得令人无法接受..因此,现在我正在寻找更新所有文档的方法一个查询..

Then I found find() + $in, and the performance is restored and even better than previous (when query), but the update is still unacceptably slow.. Therefore, now I am looking for ways to update all documents at one query..

我通常在 SQL 中做的是使用 UPDATE WHEN CASE THEN ... 例如:

what I normally do in SQL is using UPDATE WHEN CASE THEN... eg:

UPDATE person SET score = CASE
WHEN person_id = "734533604" THEN 1200
WHEN person_id = "701084015" THEN 1200
ELSE
score
END

推荐答案

您不能根据不同的条件更新多个记录,并期望upsert"能弄清楚您的意思.Upsert 标志最多可导致插入一个文档,如果您检查文档,您会发现在发生 upsert 时使用复合"更新标准是没有意义的.

You cannot update multiple records based on different criteria and expect "upsert" to figure out what you mean. Upsert flag can cause insertion of at most one document and if you check the documentation you'll see that it doesn't make sense to have a "compound" criteria for update in case of an upsert.

在您的示例中,插入应使用两个 fbid 值中的哪一个?

In your example, which of the two fbid values should the insert use?

我认为在您的情况下,您可以采取多种方法(都涉及多个操作).您可以在循环中使用 upsert 标志为每个 fbid 值调用 update 一次进行更新 - 这将按照您的预期工作,如果未找到 fbid ,则会为其创建一个新文档.其他方式涉及在运行更新之前进行查询,但我认为这些方式可能更容易出现竞争条件.

I think in your case you can take several approaches (all involve more than a single operation). You can update using the upsert flag in a loop calling update once for each fbid value - this will work like you expect and if fbid is not found a new document for it will be created. Other ways involve querying before running the update but I think those ways may be more prone to race conditions.

这里是对更新如何工作的解释 - 我发现它非常完整:http://docs.mongodb.org/手册/核心/更新/#update-operations-with-the-upsert-flag

Here is explanation of how update works - I find it pretty complete: http://docs.mongodb.org/manual/core/update/#update-operations-with-the-upsert-flag

这篇关于使用 upsert 和多语法更新 Mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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