1

I am using the mongoose updateMany() method and I also want to keep it a part of transaction. The documentation shows the example of save() where I can do something like Model.save({session: mySession}) but don't really know how to use it with for example Model.updateMany()

UPDATE: For example I have two models called SubDomain and Service and they look like this respectively:

SUB-DOMAIN

{
  name: {
    type: String,
    required: true,
  },
  url: {
    type: String,
    required: true,
    unique: true,
  },
  services: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Service",
    },
  ],
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
  },
}

SERVICE:

{
  name: {
    type: String,
    required: true,
  },
  description: {
    type: String,
    required: true,
  },
  price: { type: Number },
  tags: { type: Array },
  packages: [
    {
      name: { type: String, required: true },
      description: { type: String, required: true },
      price: { type: Number, required: true },
    },
  ],
  map: { type: String },
  isHidden: {
    type: Boolean,
    required: true,
    default: false,
  },
  sortingOrder: { type: Number },
  isForDomain: { type: Boolean, required: false, default: false },
  isForSubDomain: { type: Boolean, required: false, default: false },
  subDomains: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "SubDomain",
    },
  ],
}

Now the main field here is the services field in SubDomain and subDomains field in Service.

The complicated part: Whenever the user wants to create new service, I want to $push that service's _id into the array of services of all the subDomains inside that new service

And for that, I am using the updateMany() like this:

 const sess = await mongoose.startSession();
  sess.startTransaction();

const newService = new Service({
  _id: mongoose.Types.ObjectId(),
  subDomains: req.body.subDomains
  ...foo
})

await SubDomain.updateMany(
        { _id: { $in: req.body.subDomains } },
        { $push: { services: newService._id } }
      );

The problem starts here, of course I can do:

newService.save({session: sess})

but how do I keep my SubDomain's updateMany in the same transaction (i.e sess)

I know my example is difficult to wrap your head around but I have tried to pick a simplest example rather than copying the exact same code which would have been a lot more difficult

Tayyab Ferozi
  • 464
  • 1
  • 6
  • 20
  • here is the example https://stackoverflow.com/questions/54992810/update-many-in-mongoose – Ericgit Sep 01 '21 at 12:27
  • Here is an example of Mongoose transactions on an update operation: [Mongo DB 4.0 Transactions With Mongoose & NodeJs, Express](https://stackoverflow.com/questions/51228059/mongo-db-4-0-transactions-with-mongoose-nodejs-express) – prasad_ Sep 01 '21 at 12:44
  • Actually, I guess the reference posted by @prasad_ will solve my issue, do I need to do something like: `SubDomain.updateMany( foo, bar, { session: sess } ); }` – Tayyab Ferozi Sep 02 '21 at 03:37

0 Answers0