bulkWriteResult in mongo , nMatched and no of documents updated does not match

懵懂的女人 提交于 2019-12-11 13:13:31

问题


i using the aggregate framework for updating the user stats for all users, which is somewhere around 50k , out of which 30k have atleast single order delivered.

Now the query i am using is

var orderIds = db.delivery.find({"status": "DELIVERED"}).map(function(d){return d.order;}),
counter = 0,
bulk = db.user.initializeUnorderedBulkOp();

var userstatsCursor = db.orders.aggregate([
{ "$match": { "_id": { "$in": orderIds } } },
{ 
    "$group": { 
        "_id": "$customer", 
        "orders": { "$sum": 1 },
        "firstOrderDate": { "$min": "$dateCreated" },
        "lastOrderDate":{ "$max": "$dateCreated" } } 
    } 
}
]);

userstatsCursor.forEach(function (x){
bulk.find({ "_id": x._id }).updateOne({ 
    "$set": { 
        "totalOrders": x.orders,
        "firstOrderDate": x.firstOrderDate,
        "lastOrderDate": x.lastOrderDate
    }
});

counter++;
if (counter % 500 == 0) {
    bulk.execute(); // Execute per 500 operations and 
    // re-initialize every 500 update statements
    bulk = db.user.initializeUnorderedBulkOp();
}
});

// Clean up remaining operations in queue
if (counter % 500 != 0) { bulk.execute(); }

it find all delivered orders and then get all those customers and update their firstOrderDate, lastOrderDate and totalOrders, but the issue is

from the documentation it says that nMatched is the no of update operations , so for me i am updating all users with delivered orders, which are somewhere around 30k, but in my case it shows very less number 113 ,

Also, if i am right the no of update operations should remain same for same query even if i change the bulk size, but the query give different nMatched for different bulk size like for 600 it gives 413, for 1000 it gives 613.

Can you explain ?

来源:https://stackoverflow.com/questions/35395630/bulkwriteresult-in-mongo-nmatched-and-no-of-documents-updated-does-not-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!