I would like to know how to delete, for example, the last 100 documents inserted in my collection.
How is it possible from the shell?
You should be able to use the _id
to sort on last inserted, as outlined in the answer here:
db.coll.find().sort({_id:-1}).limit(100);
It looks like using limit on the standard mongo remove operation isn't supported though, so you might use something like this to delete the 100 documents:
for(i=0;i<100;i++) {
db.coll.findAndModify({query :{}, sort: {"_id" : -1}, remove:true})
}
See the docs for more on findAndModify
.
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