使用MongoDb更新多个记录 [英] Upsert Multiple Records with MongoDb

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

问题描述

我试图通过以下查询使MongoDB向上插入多个记录,最终使用MongoMapper和Mongo ruby​​驱动程序.

I'm trying to get MongoDB to upsert multiple records with the following query, ultimately using MongoMapper and the Mongo ruby driver.

db.foo.update({event_id: { $in: [1,2]}}, {$inc: {visit:1}}, true, true)

如果所有记录均存在,则此方法工作正常,但不会为不存在的记录创建新记录.下面的命令在shell上具有预期的效果,但在ruby驱动程序中可能并不理想.

This works fine if all the records exist, but does not create new records for records that do not exist. The following command has the desired effect from the shell, but is probably not ideal from the ruby driver.

[1,2].forEach(function(id) {db.foo.update({event_id: id}, {$inc: {visit:1}}, true, true) });

我可以从ruby中循环遍历要插入的每个id,但这将需要对每个项目进行一次数据库访问.有没有一种方法可以只通过一次数据库访问就可以从ruby驱动程序中添加多个项目?最佳做法是什么?使用mongomapper和ruby驱动程序,是否可以在一个批处理中发送多个更新,并生成如下所示的内容?

I could loop through each id I want to insert from within ruby, but that would necessitate a trip to the database for each item. Is there a way to upsert multiple items from the ruby driver with only a single trip to the database? What's the best practice here? Using mongomapper and the ruby driver, is there a way to send multiple updates in a single batch, generating something like the following?

db.foo.update({event_id: 1}, {$inc: {visit:1}}, true); db.foo.update({event_id: 2}, {$inc: {visit:1}}, true);

样本数据:

如果有两个记录,则命令后需要的数据.

Desired data after command if two records exist.

{ "_id" : ObjectId("4d6babbac0d8bb8238d02099"), "event_id" : 1, "visit" : 11 }
{ "_id" : ObjectId("4d6baf56c0d8bb8238d0209a"), "event_id" : 2, "visit" : 2 }

如果有两个记录,则命令后的实际数据.

Actual data after command if two records exist.

{ "_id" : ObjectId("4d6babbac0d8bb8238d02099"), "event_id" : 1, "visit" : 11 }
{ "_id" : ObjectId("4d6baf56c0d8bb8238d0209a"), "event_id" : 2, "visit" : 2 }

如果仅存在具有event_id 1的记录,则命令后需要的数据.

Desired data after command if only the record with event_id 1 exists.

{ "_id" : ObjectId("4d6babbac0d8bb8238d02099"), "event_id" : 1, "visit" : 2 }
{ "_id" : ObjectId("4d6baf56c0d8bb8238d0209a"), "event_id" : 2, "visit" : 1 }

如果仅存在具有event_id 1的记录,则命令后的实际数据.

Actual data after command if only the record with event_id 1 exists.

{ "_id" : ObjectId("4d6babbac0d8bb8238d02099"), "event_id" : 1, "visit" : 2 }

推荐答案

这-正确-不会插入任何具有event_id 1或2的记录(如果它们不存在的话)

This - correctly - will not insert any records with event_id 1 or 2 if they do not already exist

db.foo.update({event_id: { $in: [1,2]}}, {$inc: {visit:1}}, true, true)

这是因为查询的objNew部分(请参见 http: //www.mongodb.org/display/DOCS/Updating#Updating-UpsertswithModifiers )的字段event_id没有值.结果,您将至少需要X + 1次到数据库的行程,其中X是event_id的数量,以确保您插入一条记录(如果对于特定的event_id不存在一条记录(+1来自上面的查询) ,这会增加现有记录的访问计数器).换句话说,MongoDB如何知道要为event_id使用值2而不是1?而为什么不6?

This is because the objNew part of the query (see http://www.mongodb.org/display/DOCS/Updating#Updating-UpsertswithModifiers) does not have a value for field event_id. As a result, you will need at least X+1 trips to the database, where X is the number of event_ids, to ensure that you insert a record if one does not exist for a particular event_id (the +1 comes from the query above, which increases the visits counter for existing records). To say it in a different way, how does MongoDB know you want to use value 2 for the event_id and not 1? And why not 6?

W.r.t.使用ruby进行批量插入,我认为这是可能的,如以下链接所示-尽管我仅使用了Java驱动程序:

W.r.t. batch insertion with ruby, I think it is possible as the following link suggests - although I've only used the Java driver: Batch insert/update using Mongoid?

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

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