Update if exists, insert if it doesn’t exist – How to do Upsert in Mongoose?

To find a document and update it using Mongoose, you can use the findOneAndUpdate schema method. As the name implies, findOneAndUpdate() filter the document first that matches a given filter, applies an update, and returns the document.  Roles.findOneAndUpdate({role:req.params.role},{modules: req.body.modules}); But what if the document that is to be updated doesn’t exist? You can insert it as a new document, … Read more

MongoError: pool is draining, new operations prohibited – How to fix?

MongoError: pool is draining, new operations prohibited – How to fix? You might face this error while doing some operations on a Mongo database. mongoose.connect(url, {bufferMaxEntries: 0, reconnectTries: 5000, useNewUrlParser: true,useUnifiedTopology: true}); This error basically means that the connection that you established with the mongodb has already expired. To elaborate, you haven’t used connection pool with your … Read more