Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mongodb

Can you use mongodump to dump the latest "x" documents from a collection? For example, in the mongo shell you can execute:

db.stats.find().sort({$natural:-1}).limit(10); 

Is this same capability available to mongodump?

I guess the workaround would be to dump the above documents into a new temporary collection and mongodump the entire temp collection, but would be great to just be able to do this via mongodump.

Thanks in advance,

Michael

like image 461
Michael Bordash Avatar asked Oct 19 '11 22:10

Michael Bordash


People also ask

Does Mongodump lock the database?

Mongdump does not lock the db. It means other read and write operations will continue normally. Actually, both mongodump and mongorestore are non-blocking. So if you want to mongodump mongorestore a db then its your responsibility to make sure that it is really a desired snapshot backup/restore.

Does Mongodump preserve indexes?

Yes, mongodump does export the indexes created on the collection, and the indexes are restored with mongorestore along with the data.

Does Mongorestore overwrite?

No. From mongorestore: If you restore to an existing database, mongorestore will only insert into the existing database, and does not perform updates of any kind. If existing documents have the same value _id field in the target database and collection, mongorestore will not overwrite those documents.

What is the difference between Mongodump and Mongoexport?

mongoexport is a command-line tool that produces a JSON or CSV export of data stored in a MongoDB instance. mongodump is a utility for creating a binary export of the contents of a database.


2 Answers

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

like image 145
Mic92 Avatar answered Sep 24 '22 02:09

Mic92


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"}}}'

like image 23
Brendan Nee Avatar answered Sep 22 '22 02:09

Brendan Nee