Remove an _id field from a mongoexport json document


Warning: count(): Parameter must be an array or an object that implements Countable in /home/fbsqlcom/public_html/youdidwhatwithtsql.com/wp-content/plugins/wp-codebox/main.php on line 31

Although the mongoexport tool has a –fields option it will always include the _id field by default. You can remove this with a simple line of sed. This was slightly modified from this sed expression.

Given the following data…

{"_id":{"$oid":"57dd2809beed91a333ebe7d1"},"a":"Rhys"} 
{"_id":{"$oid":"57dd2810beed91a333ebe7d2"},"a":"James"} 
{"_id":{"$oid":"57dd2815beed91a333ebe7d3"},"a":"Campbell"}

This command-line expression will export and munge the data…

mongoexport --authenticationDatabase admin --db test --collection test -u admin -pXXXXXX | sed '/"_id":/s/"_id":[^,]*,//'

Results in the following list of documents…

{"a":"Rhys"}
{"a":"James"}
{"a":"Campbell"}

Leave a Reply