Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-1102

insertMany returns 2 types of results

    • 2

      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);
        });
      });
      

            Assignee:
            matt.broadstone@mongodb.com Matt Broadstone
            Reporter:
            kikar Gady Piazza
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: