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

查看:303
本文介绍了使用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...

我只需要更新数据库中符合特定条件的多个记录,并为不存在的记录创建新的记录.我相信我可以通过更新,更新和多个数据库访问来做到这一点.这是我想出的:

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 } );

它们都不起作用...

none of them works...

请帮助我处理这些琐碎的事情...我可以轻松地在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.

最初,我一次发现()-记录一个记录,更改值,并为修改的每个记录调用save(),但是当我部署到现场时,响应时间会变慢数百倍,特别是当每个请求有几百条记录要更新时.

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 ...例如:

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值调用一次更新-这将按您期望的方式工作,并且如果未找到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-upsert标志

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天全站免登陆