Dump first N documents of a collection with mongodump I am very new to MongoDB and I would like to know how to dump the first N elements of a collection with mongodump. I can't figure out the query I have to use.

And how would you dump the last N elements?
There is no direct way to limit number of records to export via mongodump; however it is possible indirectly, using the query parameter.

First you need to find last(n) documents you need to export.

Let's say you want to export top 50 documents. First find 50th document _id – if you are finding first(n), then sort ascending order and use $lte operator down in query.

db.col.find({},{_id:1}).sort({_id:-1}).skip(49).limit(1)


Note down the _id value.

Now pass your _id to mongodump and run it from console. (not from mongo shell)

mongodump \
--db &lt;some_db&gt; \
--collection &lt;some_col&gt; \
--query '{_id:{$gte:ObjectId("previously noted _id")}}'


Note: this solution exports the last(n) documents instead of first(n), but it is simple to adapt it.