删除时忽略限制(猫鼬)

时间:2014-03-19 15:47:11

标签: node.js mongodb mongoose

我需要删除集合中最早的几个文档,所以我写了这样的东西:

Model.remove({
    u: "abc"
}).sort({
    _id: 1
}).limit(10).exec(function (err, count) {
    // count gives the total documents number...
});

而且:

Model.find(...).sort(...).limit(...).remove(...);

希望有人能提供帮助。感谢。

2 个答案:

答案 0 :(得分:10)

MongoDB的删除操作不支持限制选项,但您可以find要删除的文档的_id,然后使用{{1}删除它们:

$in

答案 1 :(得分:1)

db.collection.remove()不会返回游标。因此,您不能像cursor methods操作一样追加db.collection.find(),如sort()或limit()。

您可能希望查看db.collection.findAndModify(),您可以提供remove参数来删除文档。