2

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

Dolev
  • 654
  • 11
  • 20

1 Answers1

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.

Daniele Tassone
  • 2,104
  • 2
  • 17
  • 25
  • 1
    I wonder if Mongoid supports it. It can be a good feature request, if not. – Jagdeep Singh Aug 20 '18 at 03:45
  • @JagdeepSingh according with MongoDB documentation 'delete' is supported by 'runCommand'. Then you should call 'runCommand' with Ruby (https://www.rubydoc.info/github/mongoid/moped/Moped%2FSession%3Acommand) to use it. – Daniele Tassone Aug 20 '18 at 05:03
  • 2
    From docs: limit - The number of matching documents to delete. Specify either a 0 to delete all matching documents or 1 to delete a single document. You can't set number of documents to delete. – Roman G. Mar 20 '20 at 12:29