I would like to locate a document, remove it and also return it:
tokens.col.remove({
token: myTokenVar
}, function (err, res) {
if (err)
throw err;
console.log(JSON.stringify(res)); // <-- this results in null
});
I'm wondering if I'm using the incorrect query type for this. Does MongoDB have such a method?
It is possible with another different command. Refer to findAndModify
command. With the options to {query: ..., remove: true, new: false}
, it will delete a single document and return the removed document.
Also there is one API findOneAndRemove
in Mongoose, Finds a matching document, removes it, passing the found document (if any) to the callback.
Added by author: Also remove .col
tokens.findAndModify({
query: {
token: myTokenVar
},
remove: true,
new: false
}, function (err, res) {
if (err)
throw err;
console.log(JSON.stringify(res));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With