简体   繁体   中英

Check if bulk is empty in mongoDB

Is there a way to check if Mongodb bulk has some operations to do before calling .execute() on it? I am pretty sure I don't send any empty objects to insert but keep getting this error on one document

Invalid Operation, No operations in bulk

Here is some code:

bulk.find({"AcctSessionId":insert['AcctSessionId']}).upsert().update({$set:insert});  

and insert object looks like this

{ AcctStatusTypeU: '3',
  AcctSessionId: '1183628512-105130252',
  h323setuptimeU: '<sip:27117929995@41.66.146.252>',
  h323connecttimeU: Sun Mar 08 2015 19:30:37 GMT+0100 (CET),
  AcmeSessionEgressRealmU: '620',
  AcmeSessionIngressRealmU: 'CORE_PSX' 
}

I see my objects inserted but still get this error. By the way this is a Nodejs driver I am talking about and I am using UNorderedBulkOp to insert documents.

I run into the same problem. Check bulk.length

if (bulk.length > 0) {
    // run bulk operations
}

You can easily check wether an bulk operation has operations to execute:

bulk

returns something like this

{ "nInsertOps" : 0, "nUpdateOps" : 1, "nRemoveOps" : 0, "nBatches" : 1 }

The reason why your bulk operation actually is empty is because your h323connecttimeU holds an illegal value – you need to quote the date string.

Furthermore, you are using the bulk operation in a wrong way. It should be

bulk.find({"AcctSessionId":insert['AcctSessionId']}).upsert().updateOne(insert);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM