SaveOrUpdate method in Mongoose with Node JS
Hello Readers, I am a Node JS developer. And developing a software on Node JS + Mongo DB. I was struggling in a situation where I need to save document in database and need to update documents if it is already present. There was a record like below: { "name":"Dummy", "age":30, "phone":"000000000" } There was a restriction like name is the UNIQUE field. Conditions : If user is going to Insert the record with the same name, then old record should be updated. Solution : Student.update( { name : 'Dummy' }, { name : 'Dummy', age : 35 }, { upsert : true }, callback ); In the above solution, we used {upsert:ture} . So this will save the new record and update if it is already exist. Thanks for reading this article. Kindly share your comments for this solution.