Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoexport without _id field

Tags:

json

mongodb

I am using mongoexport to export some data into .json formatted file, however the document has a large size overhead introduced by _id:IDVALUE tuples.

I found a similar post Is there a way to retrieve data from MongoDB without the _id field? on how to omit the _id field when retrieving data from mongo, but not exporting. It is suggested to use: .Exclude("_id"). I tried to reqrite the --query parameter of mongoexport to somehow include the .Exclude("_id") parameter, but all of the attempts failed so far.

Please suggest what is the proper way of doing this, or should I revert to using some post-export techniques?

Thanks

like image 286
Nik Avatar asked Oct 19 '12 14:10

Nik


People also ask

What is _ID field?

The _id field is immutable—that is, once a document exists in your MongoDB system, it has, by definition, been assigned an _id, and you cannot change or update its primary key. That said, _id can be overridden when you insert new documents, but by default it will be populated with an ObjectID.

Which of the following are true differences between Mongoexport and Mongodump?

One of the important differences is that mongodump is faster than mongoexport for backup purposes. Mongodump store data as a binary, whereas, mongoexport store data as a JSON or CSV.


2 Answers

There appears to be no way to exclude a field (such as _id) using mongoexport.

Here's an alternative that has worked for me on moderate sized databases:

mongo myserver/mydb --quiet --eval "db.mycoll.find({}, {_id:0}).forEach(printjson);" > out.txt 

On a large database (many millions of records) it can take a while and running this will affect other operations people try to do on the system:

like image 200
quux00 Avatar answered Sep 20 '22 00:09

quux00


This works:

mongoexport --db db_name --collection collection_name | sed '/"_id":/s/"_id":[^,]*,//' > file_name.json 
like image 28
Gayatri Tirodkar Avatar answered Sep 20 '22 00:09

Gayatri Tirodkar