I’m learning Mongoose and I can’t find in the documentation, nor in other questions on this site, why updateMany()
needs to have a .then()
in the end to be executed.
As an example, here is the code I’m trying to run, aiming to change the rating of the movies made in 1992 and 2019.
Movie.updateMany({year: {$in: [1992,2019]}}, {rating: 'M'}).then(data => console.log(data));
These are the movies in question:
{ "_id" : ObjectId("6282e0d37a9a5a4851d465b1"), "title" : "Elton Pan", "year" : 2013, "score" : 10, "rating" : "M", "__v" : 0 }
{ "_id" : ObjectId("6282e0d37a9a5a4851d465b2"), "title" : "Pon Porom", "year" : 2019, "score" : 0, "rating" : "M", "__v" : 0 }
{ "_id" : ObjectId("6282e0d37a9a5a4851d465b3"), "title" : "The Night", "year" : 1992, "score" : 10, "rating" : "R", "__v" : 0 }
If I attempt to run the code without .then
in the end, nothing changes.
I have also seen people use this method without .then
in the end, which makes me wonder if I can’t use it this way because of the version of Mongoose I’m using.
Just to clarify, I’m checking the results via the Mongo shell, and not attempting to retreive the results within the code.
Thanks in advance, and sorry for the noob question 🙂
3
Answers
I think I may have found an explanation to the
.then
question. Quoting Zarko, a teacher in the (Udemy) course I'm doing:So, basically, the
.then
or.exec
is necessary to trigger the execution of that query (when not usingasync...await
). At least that is what I understand from the teacher's answer.This is my first post, and I'm not sure if I should have posted this as an answer or edited my own question to include this explanation, feedback would be appreciated. Thanks.
It doesn’t need a
.then()
to be executed. It executes just fine when you do this:You just don’t have access to the results yet because it’s an asynchronous operation. The function returns a promise. The
.then()
waits for the promise to resolve, then gives you the result as the.then()
callback’s first parameter.I’d recommend using
async...await
syntax. Having a bunch of.then()
promise chaining crap is no good, especially nowadays.I learned from the post of @Mats below that
updateMany
does not work without.then
because it returns a Mongoose Query Object, not the Promise meant to update the data. They have a.then()
function for co and async/await as a convenience. If you need a fully-fledged promise, use the.exec()
function. Read the post of @Mats below for more.My answer when I thought it returns the Promise: