我想了解您对我设置的流程的意见:
我有两个集合:
Clients : { client_id, client_type }
Orders : { order_id, client_id,order_amount, order_valid }
根据这些集合,我想计算按客户类型划分的有效订单数量等。
所以我想加入这两个集合并将 client_type 添加到我的订单集合中
我这样做了,但我发现这个解决方案不是很有效。
db.clients.find (
{},
{
client_id : 1,
client_type : 1
}
).forEach(function (pClient) {
db.orders.update (
{
client_id : pClient.client_id
},
{ $set : {
client_type : pClient.client_type
}},
{
multi : true
}
)
});
您知道如何改进这个过程吗?
最佳答案
所以,这是我发现的最快的解决方案:
var bulk = db.orders.initializeUnorderedBulkOp();
var bulk_counter = 0;
db.clients.find (
{},
{
client_id : 1,
client_type : 1
}
).forEach(function (pClient) {
bulk.find(
{
client_id : pClient.client_id
}
).update(
{ $set : {
client_type : pClient.client_type
}},
)
bulk_counter++;
if (bulk_counter % 1000 == 0)
{
bulk.execute();
bulk = db.m_recipient_indicators.initializeUnorderedBulkOp();
}
});
if (bulk_counter % 1000 != 0)
{
bulk.execute();
}
关于javascript - MongoDB 加入/更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35313335/