jsonmongodbbsonmongoexport

In what cases mongoexport can be used?


From the documentation, it says to avoid mongoexport on BSON datatypes

WARNING Avoid using mongoimport and mongoexport for full instance production backups. They do not reliably preserve all rich BSON data types, because JSON can only represent a subset of the types supported by BSON. Use mongodump and mongorestore as described in MongoDB Backup Methods for this kind of functionality.

Created a collection with name "testCollection"

 > db.testCollection.insert({title: 'MongoDB Overview',
... description: 'MongoDB is magical database',
...    by: 'by newbie',
...    url: 'http://www.mongodb_cannot_understand_mongoexport.com',
...    tags: ['mongodb', 'database', 'NoSQL'],
...    likes: 100});

> db.testCollection.find().pretty();
{
    "_id" : ObjectId("59524e6412d3ef3c879c267a"),
    "title" : "MongoDB Overview",
    "description" : "MongoDB is magical database",
    "by" : "by newbie",
    "url" : "http://www.mongodb_cannot_understand_mongoexport.com",
    "tags" : [
        "mongodb",
        "database",
        "NoSQL"
    ],
    "likes" : 100
}

Executing the below commands gives type as object and String,

typeof db.testCollection.findOne()._id; output : object

typeof db.testCollection.findOne().title; output : string

If running mongoexport on the above collection may or mayn't guarantee the preservation of data as it contains the data types string and object. (I doubt any documents will not have string, objectID datatype)

In that case mongoexport should not be used at all from this list of bson types link description here

My question is

In what cases mongoexport can be used with a example collection ?

NOTE: I want to use mongoexport, mongodump is not an option


Solution

  • What that paragraph in the documenation means is that, with mongoexport/mongoimport, there's no guarantee that data in the restored database will match exactly what was in the original source. This is because JSON does not natively support many of BSON data types, such as Date, MinKey, ObjectId, etc. So mongoexport has to do workarounds to export at least something for those types. This is how it exports ObjectId fields, for example:

    "_id": {"$oid": "531701fdb9e3b40002000002"}
    

    To backup/restore your data reliably, use mongodump/mongorestore.

    In what cases mongoexport can be used with a example collection ?

    When you want to get a somewhat human-readable snapshot of your data. JSON is also very well supported in most every language, so you can use mongoexport to dump data for crunching with python's scientific libs, or something like that.