问题
Using mongoid, how to delete the first 10000 documents where the error_message
field is: Error: not found
.
Native mongo queries will be happily accepted
回答1:
MongoDB support limit on delete.
The delete command removes documents from a collection. A single delete command can contain multiple delete specifications
{
delete: <collection>,
deletes: [
{ q : <query>, limit : <integer>, collation: <document> },
{ q : <query>, limit : <integer>, collation: <document> },
{ q : <query>, limit : <integer>, collation: <document> },
...
],
ordered: <boolean>,
writeConcern: { <write concern> }
}
https://docs.mongodb.com/manual/reference/command/delete/#dbcmd.delete
where 'q' is your query with your specific input data (that must match the documents you want to delete) and 'limit' is the number of max documents to delete. As you can see, there is the possibility to have multiple delete conditions but is out of the scope of your question.
来源:https://stackoverflow.com/questions/51917027/mongoid-delete-many-with-limit