Could you please tell if it’s possible to get number of inserted documents using bulk deleted?
For some, reason batchInsert it always returns:
array
‘err’ => null
‘n’ => int 0
‘ok’ => float 1
The code is something like this using mongodb shanty:
$result = Model_Mongodb::insertBatch($importData, array('safe' => true));
Unfortunately, MongoDB doesn’t return the number of documents inserted by an insert operation. This is a known issue — please see https://jira.mongodb.org/browse/SERVER-4381, and vote for the issue if this is important for your use case.
However, I will note that if you are using
insertBatchwith safe mode and you do not get an error result, you may assume that all the documents were inserted successfully.EDIT (to answer questions from comments):
By default, a bulk insert stops if there is an error (and returns the error information in the return result). You can override this behavior in MongoDB 2.0+ with the
continueOnErrorflag, in which case the return result will only give information about the last error; any earlier errors will not be reported.No, you cannot do a bulk upsert. To do an upsert (create if missing, otherwise update), you must use the
updatemethod with theupsertflag set to true.That ticket is not strictly about
batchInsert, no — allinsertcommands currently return n==0 in thegetLastErrorresult.