UpdateOne returns a mongoose object and not document

ATTENTION !

I saw the responses, they are all right, BUT, pay attention to add the new option and to set it as true into the query to get the new updated document, otherwise it will return back the document before updating:

    models.Projects.findByIdAndUpdate(
    { "_id": projectId }, 
    { $addToSet:{ membersIds: { $each: membersIds }}})
   .exec()
   .then((response) => {
          console.log('document before updating =>', response);
        });

    models.Projects.findByIdAndUpdate(
        { "_id": projectId }, 
        { $addToSet:{ membersIds: { $each: membersIds }}}, 
        {new: true})
       .exec()
       .then((response) => {
              console.log('document after updating =>', response);
            });

From the docs: [callback] «Function» params are (error, writeOpResult) which means that there's no way to return a document with updateOne method.

Instead of updateOne it's however possible to use findByIdAndUpdate providing lean: true option

Tags:

Mongoose