Dolev
Dolev

Reputation: 684

Mongoid delete many with limit

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

Upvotes: 2

Views: 4747

Answers (1)

Daniele Tassone
Daniele Tassone

Reputation: 2174

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.

Upvotes: 1

Related Questions