I might be doing it wrong with Mongoose after using it for a while. I find it difficult to read when you have too much callbacks, eg:
I want to find a document and update it. I need to find it first, then the update will be inside the callback of the findOne.
var sam = new Character({ name: 'Sam', inventory: {}});
Character.findOne({ name: 'Sam' }, function(err, character) {
console.log(character);
// now I want to update it.
character.update({... }, function(err, characterID) {
// now I want to know the size of the document after the update.
Character.findOne({ _id: characterID }, function(err, character) {
// Now update it again....
});
});
});
It ends up a spaghetti code eventually!
You see what I mean?
Any ideas how to do it better?
Or mongodb native is better without all these callbacks?
EDIT:
Character.findOne({...}, function(err, character) {
return character.update(...);
}).select("-field1 -field2").then(function(data) {
//
}).catch(function(error) {
// Handle any error from all above steps
}).done();
Error:
TypeError: Character.findOne(...).select(...).then(...).catch(...).done is not a function
findOneAndUpdate.