Is it possible to mongodump the last "x" records from a collection?

mongodump supports the --query operator. If you can specify your query as a json query, you should be able to do just that.

If not, then your trick of running a query to dump the records into a temporary collection and then dumping that will work just fine. In this case, you could automate the dump using a shell script that calls a mongo with a javascript command to do what you want and then calling mongodump.


Building off of Mic92's answer, to get the most recent 1000 items from a collection:

Find the _id of the 1000th most recent item:

db.collection.find('', {'_id':1}).sort({_id:-1}).skip(1000).limit(1)

It will be something like 50ad7bce1a3e927d690385ec.

Then pass this _id in a query to mongodump:

$ mongodump -d 'your_database' -c 'your_collection' -q '{"_id": {"$gt": {"$oid": "50ad7bce1a3e927d690385ec"}}}'


mongodump does not fully expose the cursor interfaces. But you can work around it, using the --query parameter. First get the total number of documents of the collection

db.collection.count()

Let's say there are 10000 documents and you want the last 1000. To do so get the id of first document you want to dump.

db.collection.find().sort({_id:1}).skip(10000 - 1000).limit(1)

In this example the id was "50ad7bce1a3e927d690385ec". Now you can feed mongodump with this information, to dump all documents a with higher or equal id.

$ mongodump -d 'your_database' -c 'your_collection' -q '{_id: {$gte: ObjectId("50ad7bce1a3e927d690385ec")}}'

UPDATE The new parameters --limit and --skip were added to mongoexport will be probably available in the next version of the tool: https://github.com/mongodb/mongo/pull/307

Tags:

Mongodb