在我的集合中,文档包含状态和时间戳之类的键。当我想找到最新的10个文档时,我会编写以下查询
db.collectionsname.find().sort({"timestamp"-1}).limit(10)这个查询给出了我想要的结果,但是当我想删除最新的10个文档时,我编写了下面的查询
db.collectionsname.remove({"status":0},10).sort({"timestamp":-1})但是它显示了下面的错误TypeError: Cannot call method 'sort' of undefined,我再次编写了与db.collectionsname.remove({"status":0},10)下面相同的查询,它只删除一个文档。那么,如何编写一个查询,删除10个最新文档并在时间戳上进行排序?
发布于 2019-04-02 09:42:46
删除集合myCollection中的N个文档
db.getCollection('myCollection').find({}).limit(N).forEach(function(doc){
db.getCollection('myCollection').remove({_id: doc._id});
})https://stackoverflow.com/questions/19065615
复制相似问题