Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to give limit in mongoexport in mongodb

Tags:

mongodb

I want to take backup of only 100 records from a collection how can i give limit in query -q.

here what i tried giving limit but after giving limit in -q it is not inserting any record.

mongoexport --host localhost --db test --collection foo  -q "{"limit":"1"}" > a.json

Please suggest how can i give limit number of record in mongoexport

http://www.mongodb.org/display/DOCS/mongoexport

like image 588
XMen Avatar asked Oct 03 '11 07:10

XMen


People also ask

How do I limit a record in MongoDB?

The Limit() Method To limit the records in MongoDB, you need to use limit() method. The method accepts one number type argument, which is the number of documents that you want to be displayed.

Does MongoDB have a limit?

The maximum size an individual document can be in MongoDB is 16MB with a nested depth of 100 levels. Edit: There is no max size for an individual MongoDB database.

What is the limitation of a document in MongoDB?

The maximum BSON document size is 16 megabytes. The maximum document size helps ensure that a single document cannot use excessive amount of RAM or, during transmission, excessive amount of bandwidth. To store documents larger than the maximum size, MongoDB provides the GridFS API.

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

mongoexport supports limit, so simply:

mongoexport --host localhost --db test --collection foo  --limit 1 --out a.json
like image 147
JAR.JAR.beans Avatar answered Nov 05 '22 18:11

JAR.JAR.beans


If you are using a *nix (or cygwin), you could use head -100

mongoexport --host localhost --db test --collection foo  | head -100 > a.json

EDIT : as found on the mongodb mailing list, there is a feature request for this : https://jira.mongodb.org/browse/SERVER-2033

EDIT: this answer is more than 4y old. Mongodb has implemented a built-in feature, see other answers below.

like image 21
kamaradclimber Avatar answered Nov 05 '22 18:11

kamaradclimber