findbyIdAndUpdate not working when there is addition of records in collection


I have wrote a update query which will find the record by _id and update. This code is completely working fine until If I add a new record in that collection. If add a record and do a query it goes to success part but returns null instead of the data. But when I restart the code and try same operation, it’s working fine. My method is PUT.




Usually, we can update a single record in multiple ways

  1. findOneAndUpdate() method is used for update a record in collection.
    It is okay to use this method, instead of manually finding the record and then updating it.
    This method works really faster than the manual method.
    It has an option flag new, if it is true it will return an updated record in the response.

  1. findByIdAndUpdate() method is used to update record based on Id.
    Where we can pass the id as «Object|Number|String» value of _id to query. So here, directly we can pass

Leave a Reply