-
Type: Bug
-
Resolution: Works as Designed
-
Priority: Major - P3
-
None
-
Affects Version/s: 2.2.30
-
Component/s: MongoDB 3.2, MongoDB 3.4
-
2
-
Empty show more show less
Collection.insertMany returns one type of result when all inserts are correct:
INSERT RESULT { result: { ok: 1, n: 2 }, ops: [ { _id: 1 }, { _id: 2 } ], insertedCount: 2, insertedIds: [ 1, 2 ] }
When some of the operations fail, a BulkWriteResult is returned:
INSERT RESULT BulkWriteResult { ok: [Getter], nInserted: [Getter], nUpserted: [Getter], nMatched: [Getter], nModified: [Getter], nRemoved: [Getter], getInsertedIds: [Function], getUpsertedIds: [Function], getUpsertedIdAt: [Function], getRawResponse: [Function], hasWriteErrors: [Function], getWriteErrorCount: [Function], getWriteErrorAt: [Function], getWriteErrors: [Function], getLastOp: [Function], getWriteConcernError: [Function], toJSON: [Function], toString: [Function], isOk: [Function] }
The code to show the bug is this, just run it twice:
const { MongoClient } = require('mongodb'); MongoClient.connect('mongodb://localhost:27017/test', (err, db) => { if (err) { return console.log('MONGO CONNECTION ERR'); } db.collection('users').insertMany([{_id: 1}, {_id: 2}], (err, result) => { if (err) { console.log('WRONG INSERT', err); } console.log('INSERT RESULT', result); }); });