bulkWriteResult in mongo , nMatched and no of docu

2019-08-08 19:37发布

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 ?

0条回答
登录 后发表回答