Mongo删除最后的文件 [英] Mongo remove last documents
本文介绍了Mongo删除最后的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我想知道如何删除收藏夹中插入的最后100个文档.
I would like to know how to delete, for example, the last 100 documents inserted in my collection.
从外壳上怎么可能?
推荐答案
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);
看起来像在标准mongo删除操作上使用限制但是不支持,因此您可以使用类似的方法删除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})
}
有关findAndModify
的更多信息,请参见文档.
See the docs for more on findAndModify
.
这篇关于Mongo删除最后的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文