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

insertMany returns 2 types of results

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Works as Designed
    • Affects Version/s: 2.2.30
    • Fix Version/s: None
    • Component/s: MongoDB 3.2, MongoDB 3.4
    • Labels:
      None
    • Story Points:
      2

      Description

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

        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated:
              Resolved: