1 / 1
Apr 9

Title:
Mongoose create() with session throws error: “Cannot call create() with a session and multiple documents unless ordered: true is set”

Body:
I’m using mongoose@8.13.2 with mongodb@6.15.0. I’m trying to use the create() method in Mongoose along with a session for transaction support. However, when I try to insert multiple documents, I get the following error:

“Cannot call create() with a session and multiple documents unless ordered: true is set”

Here is the relevant part of my code:

async create( payload: Array<Partial<OrderDocument>>, options?: CreateOptions, ) { try { return this.orderModel.create(payload, { ...options, ordered: true }); } catch (error) { throw error; } }

Context:

  • I’m passing an array of documents in payload.
  • I use a session in options for transactions (i.e., options = { session }).
  • The error seems to happen only when using both a session and multiple documents.

What I’ve tried:

  • Adding { ordered: true } to the options, as the error message suggests. That seems to solve the problem.
  • But I’m wondering why this is required only when a session is present.
  • Is this a new behavior in Mongoose 8 or MongoDB 6?

Questions:

  1. Why is ordered: true necessary when using create() with session and multiple documents?
  2. Is this the recommended approach or is there a better way to bulk insert with session support?
  3. Will this affect performance or the way errors are handled in bulk inserts?

Any insights or official documentation links would be appreciated!