I am trying MongoDB transactions with mongoose but on every request to create a database and insert a user and a company, I am getting the following error
"Unable to read from a snapshot due to pending collection catalog changes; please retry the operation. Snapshot timestamp is Timestamp(1584451745, 1). Collection minimum is Timestamp(1584451753, 1)"
On retrying the request it works. But When I send a new request to create a new database and insert a user and a company, I get the same error. I always need to retry the request to successfully insert the data.
const session = await mongoose.startSession();
session.startTransaction();
try {
await db.createCollection("users");
await db.createCollection("companies");
const savedUser = await UserModel({
...userDetails,
}).save({ session });
const company = await new CompanyModel({
...companyDetails
}).save({ session });
await session.commitTransaction();
return { message: "User Added Successfully" };
} catch (err) {
error("Transaction Error", err);
await session.abortTransaction();
throw err;
} finally {
session.endSession();
}
Any idea what I am missing here?