title: mongoose how to send transaction multiple collections
body: <p><strong>Side-Note</strong> I connect to DB with the following code:</p>
  <pre class="lang-js prettyprint-override"><code>const mongoose = require('mongoose');
  
  const connectDB = (url) =&gt; {
    return mongoose.connect(url);
  }
  
  </code></pre>
  <p><strong>Problem Description:</strong>
  I have two different Collections. Both Operations, <code>findByIdAndUpdate</code> and <code>create</code> must run as an atomic operation. This should be possible with mongoose Transactions.</p>
  <pre class="lang-js prettyprint-override"><code>const registerCustomer = async (req, res) =&gt; {
     await CustomerRegistrationCode.findByIdAndUpdate(req.body._id, { used: true });
     const customer = await Customer.create({firstName: req.body.firstName});
  }
  </code></pre>
  <p><strong>What I tried:</strong></p>
  <pre class="lang-js prettyprint-override"><code>const registerCustomer = async (req, res) =&gt; {
      const session = await mongoose.startSession();
      await session.startTransaction();
      try {
          await CustomerRegistrationCode.findByIdAndUpdate(req.body._id, { used: true }); //updates even though 
          const customer = await Customer.create({ firstName: req.body.firstName });// this line will throw error
          await session.commitTransaction();
          session.endSession();
      } catch (error) {
          console.error('abort transaction');
          await session.abortTransaction();
          session.endSession();
          throw error;
      }
  }
  
  </code></pre>
  <p><strong>Problem</strong> The CustomerRegistrationCode Collection gets updated even though the Customer.create method throws an error. How can this be solved?</p>
  <p><strong>New approach to understand MongoDB Transactions</strong> fails, but this is official code from <a href="https://mongoosejs.com/docs/transactions.html" rel="nofollow noreferrer">https://mongoosejs.com/docs/transactions.html</a></p>
  <pre class="lang-js prettyprint-override"><code>const mongoose = require('mongoose');
  const debugMongo = async () =&gt; {
    const db = await mongoose.createConnection(&quot;mongodb://localhost:27017/mongotest&quot;);
    const Customer = db.model('Customer', new mongoose.Schema({ name: String }));
    const session = await db.startSession();
    session.startTransaction();
    await Customer.create([{ name: 'Test' }], { session: session }); //(node:20416) UnhandledPromiseRejectionWarning: MongoServerError: Transaction numbers are only allowed on a replica set member or mongos
    let doc = await Customer.findOne({ name: 'Test' });
    assert.ok(!doc);
    doc = await Customer.findOne({ name: 'Test' }).session(session);
    assert.ok(doc);
    await session.commitTransaction();
    doc = await Customer.findOne({ name: 'Test' });
    assert.ok(doc);
    session.endSession();
  }
  
  debugMongo();
  
  </code></pre>
  <p>At <code>Customer.create</code> an error gets thrown and i don't know why. Does somebody have an minimal working example?</p>