Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:[email protected]>',
  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.

like image 700
v0d1ch Avatar asked Apr 23 '15 08:04

v0d1ch


People also ask

How do I check if a field is empty in MongoDB?

In MongoDB, we can check the existence of the field in the specified collection using the $exists operator. When the value of $exists operator is set to true, then this operator matches the document that contains the specified field(including the documents where the value of that field is null).

What is bulk in MongoDB?

Bulk() Bulk operations builder used to construct a list of write operations to perform in bulk for a single collection. To instantiate the builder, use either the db. collection. initializeOrderedBulkOp() or the db.

What is bulk operation?

Bulk operations are actions that are performed on a large scale. Example bulk operations tasks include importing or exporting items, changing prices on a mass scale, and assigning products to a warehouse. For each individual task of a bulk operation, the system creates a message that is published in a message queue.

What is the principal implication of using a bulk write operation in MongoDB?

bulkWrite() method provides the ability to perform bulk insert, update, and delete operations. MongoDB also supports bulk insert through the db. collection.


1 Answers

I run into the same problem. Check bulk.length

if (bulk.length > 0) {
    // run bulk operations
}
like image 136
Mike Avatar answered Oct 03 '22 19:10

Mike