大众更新如何/插入S. mongoid/mongodb?

我有一个包含数百万个文件的数据库
Order

. 我pacito以下列方式插入:


Order.collection.insert/[
{:_id=>BSON::ObjectId/'5471944843687229cdfb0000'/, :status=>"open", :name=> "Benny"},
{:_id=>BSON::ObjectId/'5471944843687229cdfc0000'/, :status=>"open", :name=> "Allan"}
]/


我需要定期更新属性
status

在订单中。 使用该方法将它们单独更新它们是非常效力的更新
update_attribute

.

我如何大量更新几个文件 MongoDB?

可以使用下面的代码最佳地描述所需的解决方案。 "fictional":


# IMPORTANT: The exemplified upsert method does not exist

Order.collection.upsert/[
{:_id=>BSON::ObjectId/'5471944843687229cdfb0000'/, :status=>"closed"},
{:_id=>BSON::ObjectId/'5471944843687229cdfc0000'/, :status=>"some_other_status"}
]/


供你参考 ,
https://coderoad.ru/25550690/
SO 可以是一个类似的问题/答案,但是,老实说,我不遵循答案。
已邀请:

君笑尘

赞同来自:

之前可以简化指定问题中的最佳答案


id_status = [['5471944843687229cdfb0000','closed'], ...] 

bulk_order = id_status.map do |id, status| # Using array destructuration
{ update_one:
{
filter: { _id: id },
update: { :'$set' => {
status: status,
}}
}
}
end
YourCollection.collection.bulk_write/bulk_order/

帅驴

赞同来自:

首先,您需要过滤
Orders

仅适用于那些巧合的标识符
orders_to_update

.
您使用过滤它们
http://two.mongoid.org/docs/qu ... ny_in
any_in . 然后用批量更新它们
http://mongoid.org/en/mongoid/docs/querying.html
.

像这样:


orders_to_update = [BSON::ObjectId/'5471944843687229cdfb0000'/, BSON::ObjectId/'5471944843687229cdfc0000'/]

Order.any_in/id: orders_to_update/.update_all/status: "closed"/

涵秋

赞同来自:

这个问题在这里是

更新

. 更新缓慢发生,因为它必须读取,替换和更改文档。

许多天我被封锁了同样的问题。 我没有找到任何解决方案 stackoverflow, 既不是其他网站。 因此,我写了自己的决定。 也许你会发现它不是很好 "clean", 但它适用于出色的时间结果。

解决方案是再次销毁并创建此文档。

. 破坏 - 它非常快,并使用大规模执行创建新文件 "collection.insert" - 非常快。


def get_orders/*params/
Order.where/# some conditions/.asc/:id/
end

namespace :my_collection_repairer do
desc ""

task update: :environment do
all_orders = get_orders/# some conditions/
while all_orders.count > 0
num_docs = all_orders.count
group_size = 10000
num_groups = /Float/num_docs/ / group_size/.ceil
puts "#{num_docs} documents found. #{num_groups} groups calculated."

1.upto/num_groups/ do |group|
updated_order_list = []
order_group = all_orders.page/group/.per/group_size/
puts "group #{group}"

order_group.each do |order|
updated_order = update_order/order/ # this represents your custom update method
updated_order_list << updated_order.as_document
order.destroy
end

Order.collection.insert/updated_order_list/
puts "Group #{group} updated."
end
all_orders = get_orders/# some conditions/
end
end
end

喜特乐

赞同来自:

设置参数 upsert 价值 true 更新或更换并具有以下内容

句法

:


bulk.find/ { status: "closed" } /.update/ { $set: { status: "some_other_status" } } /;
bulk.execute//;


将多个更新操作添加到质量操作列表中。 该方法更新现有文档中的某些字段。

使用

方法 Bulk.find//, 指定确定应更新哪些文档的条件。

方法 Bulk.find.update// 更新所有相关文件。 要指定文档的一个更新,请参阅

Bulk.find.updateOne//

.


var bulk = db.collection.initializeUnorderedBulkOp//;
bulk.find/ { status: "closed" } /.upsert//.update/
{
$set: { status: "some_other_status"}
}
/;
bulk.execute//;


笔记

:

以表示 upsert: true 对于此操作,使用

Bulk.find.upsert//

. 在

案子

Bulk.find.upsert//, 如果没有文件对应

健康)状况

要求 Bulk.find//, 更新操作只插入一个文档。
我希望它会有所帮助。

要回复问题请先登录注册