codewarrior
codewarrior

Reputation: 863

How to export desired number of documents from a mongo collection

I know we can take mongo dump of a collection . I would like to know whether it is possible to take dump of only required number of documents from a mongo collection. My catalog collection have hundreds of thousands of documents , however I need only recent 2000 documents as a dump for sync purpose.

Upvotes: 1

Views: 928

Answers (1)

oblivion
oblivion

Reputation: 6548

Yes, you can limit the number of documents from a collection using --query option.Inside the query, you could sort the documents by created_date(since you want recent documents only, you must have a similar field in your document, do not try to sort by _id because in some case,ObjectId values do not represent a strict insertion order) and limit documents to 2000 using the query.

But, mongodump --query doesn't directly support the sort and limit operation. So, you need to do some workarounds in your query.

Firstly, you could fetch the 2000th record, by sorting on the basis of your created_date in ascending order as:

db.collection.find().sort({created_date:1}).skip(1999).limit(1)

This will give you the 2000th record. Inside that record, you will also have the value of created_date. Now using that value, you could run mongodump as:

mongodump --db <db-name> --collection <collection-name> --query '{"created_date":{$lte:{$date:<value-in-milliseconds>}}}'

Note that, you need to specify date in milliseconds here, because --query doesn't support ISO dates directly.

For more information, see --query

UPDATE

However, you could use ISO dates in the query by specifying query in a separate file, for eg: query.json as:

{"created_date": {"$lte": < ISODate("...")-value-of-your-created_date >}}

Then you could do mongodump as:

mongodump --db <db-name> --collection <collection-name> --queryFile query.json

For more information, see --queryFile

Upvotes: 2

Related Questions