Insert if data doesn’t exist in the document – How to do insert new field and data with Mongoose? Insert if data doesn’t exist in the document – How to do insert new field and data with Mongoose? mongoose mongoose

Insert if data doesn’t exist in the document – How to do insert new field and data with Mongoose?


  • change in option upsert: false
  • one major problem is field name "owner.login" this is causing the issue, this will search in object owner: { login: userId } but in actual we have string "owner.login", so this is not able to find any matching document, and this will not update record.
  • you can check after removing condition on field owner.login, it will work
Repo.findOneAndUpdate({    'name': pageId  },   { detailViewCounter: 0 },  { new: true, upsert: false })

Look at MongoDB restriction on field names, and good question in SO.