I’m trying to learn the Mongoose ORM for MongoDB and node.js. I want to query documents in my database, perform an action on each one, and then perform another action after all of the previous actions are complete. In other words, I want to do something like this, assuming I have a model called ‘User:’
var userArray = [];
User.find({})
.each(function(user) {
user.age += 1;
userArray.push(user);
})
.then(function() {
//do something with userArray
//happens after all the users' ages are modified
});
What is the correct syntax for doing something like this in Mongoose?
If you only need to perform synchronous actions for each document, the solution is fairly simple. (console.warn() is synchronous, so you can use it to verify that all user objects are processed first.)
If you need to perform some sort of asynchronous action (perhaps another db query), the solution becomes more complex. I encountered this issue recently and briefly debated using a module such as Step or rolling my own barebones solution. Step provided more functionality than I needed, so I decided that the added overhead was not worth it. Here is my solution:
Please pardon the obfuscation (I yanked that right from the source of a project that is under-wraps). Essentially, you wait until the final async operation to complete.