How to Export MongoDb GridFs Data(files and chunk also) after perticular date

1,425 views
Skip to first unread message

Naveen Vyas

unread,
Jul 23, 2018, 7:25:04 PM7/23/18
to mongodb-user
I generated some GridFs data on local MongoDB but I want to export these files only to production MongoDB.

I just have uploadtime field  to query these files. Is there some option in Export to JSON by mongoexport both files and chunks after time stamp.

Kevin Adistambha

unread,
Jul 31, 2018, 8:55:07 PM7/31/18
to mongodb-user

Hi Naveen

Is there some option in Export to JSON by mongoexport both files and chunks after time stamp

Generally unless you need a textual JSON representation of documents, it’s preferable to use mongodump and mongorestore instead, since those tools dumps/restores using MongoDB-native BSON format. This will make those dumps faster to create and faster to load.

To limit the dump to specific documents, mongodump supports the --query parameter.

Having said that, GridFS consists of two collections: fs.files and fs.chunks. While it is possible to query the fs.files collection for e.g. date created metadata, those same metadata is currently not available in the fs.chunks collection. So some manual coding would be required to determine which documents in the fs.chunks collection that are relevant.

Another possible solution is to prepare the fs.files and fs.chunks collections in your local deployment for exporting by removing unwanted files, so that you can do a simple mongodump operation without worrying about querying and manual intervention.

Best regards
Kevin

Naveen Vyas

unread,
Aug 1, 2018, 3:08:32 AM8/1/18
to mongodb-user
Thanks Kevin,
 I have already done this exercise on local mongoDB Server and created 2 json file to be imported on production server for fs.files and fs.chunks respectively by --query parameter with mongoexport command .
 Perhaps you answered after long time. Anyway but i want to ask now, what is difference between mongoexport/import and mongodump/restore? which is more beneficial and faster?
 I fetched my required chunks on querying on ObjectID value.   by &gte sign and i got required files/chunks.

Kevin Adistambha

unread,
Aug 1, 2018, 11:46:06 PM8/1/18
to mongodb-user

Hi Naveen,

what is difference between mongoexport/import and mongodump/restore

MongoDB uses BSON, which is a binary-encoded serialization of documents. One advantage of BSON compared to JSON is the existence of extended datatypes, which JSON doesn’t have.

mongoexport will output documents in JSON format. Similarly, mongoimport will import JSON documents into your MongoDB instance. These tools are needed for interoperability between JSON & BSON. Note that in both tools, the input/output format is text (structured as JSON). This necessitates a transformation between MongoDB-internal BSON format into text

In contrast, mongodump and mongorestore inputs & outputs BSON (binary). If your intent is to create a backup of your MongoDB instance, using those tools avoids the transformation step of mongoimport & mongoexport, thus the process is generally faster. Also note that both tools also dumps and restores indexes in the collection, which mongoimport & mongoexport doesn’t do.

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages