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

Improve insertMany error when provided a nested array

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: CRUD
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      `insertMany` uses bulkWrite internally.  when a bulk write operation is constructed, we add each bulk write operation to "batches" and throw an error if the operation is not a document (specifically, checking if it's an array and throwing if it is).  because `insertMany` uses bulkWrite, if a user provided a nested array in side an array of documents when inserting, they receive a rather unhelpful error from bulk write:

      const client = new MongoClient(process.env.MONGODB_URI!);
      
      async function newFunction() {  
        await client.connect();
        await client
          .db('foo')
          .collection('bar')
          .insertMany([{ name: 'bailey' }, [{ name: 'bailey ' }]]);
      }
      
      newFunction();
      
      // MongoInvalidArgumentError: Operation passed in cannot be an Array

      It would be nice if we threw a more helpful error here instead, such as "MongoInvalidArgumentError: insertMany expects an array of documents, element <> was an array".

      Acceptance Criteria:

      • Detect invalid arguments in insertMany and throw a helpful error from insertMany if a non-document element is passed into insertMany

            Assignee:
            Unassigned Unassigned
            Reporter:
            bailey.pearson@mongodb.com Bailey Pearson
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: