mongoexport fields from subdocuments to csv

2019-01-15 17:21发布

问题:

I am trying to export a field from a subdocument with no luck.
Here is my syntax;

mongoexport -d test -c accounts -f account_number,situses.coordinates -o coordinates.csv --type=csv  

The output includes the account_number but not the coordinates field from the subdocument. According to the docs, this is supposed to work.

The following will export the entire situses subdocument, but I only want the one field.

mongoexport -d test -c accounts -f account_number,situses -o coordinates.csv --type=csv

Am I just referencing the subdocument field wrong or something?

I'm running Mongodb 3.0.4

ADDITIONAL INFO

The following syntax worked on an earlier version of Mongodb (2.6.x ?). Notice the subdoc.0.fieldname syntax.

mongoexport -d test -c accounts -f account_number,situses.0.coordinates -o coordinates.csv --csv  

It appears support for directly referencing a subdocument has been removed.

回答1:

There is mistake in your syntax.

From mongo version 3.0.0, mongoexport removed the --type = csv option. Use the --type=csv option to specify CSV format for the output.

You should use :

mongoexport --db tests --collection accounts --type=csv --fields account_number,situses --out coordinates.csv

For nested fields you should use :

mongoexport --db tests --collection accounts --csv --fields 'account_number,situses.0.coordinates'  --out /home/vishwas/c1.csv 

EDIT for mongo 3.0 with sub documents:

You need to create separate collection with required fields from subdocuments like -

db.test.aggregate({"$unwind":"$situses"},{"$project":{"_id":0,"account_number":1,"siteUsesCo":"$situses.coordinates"}},{"$out" : "forcsv"}) 

If you want only one field from subdocument then use aggregation like -

db.test.aggregate({"$unwind":"$situses"},{"$limit":1},{"$project":{"_id":0,"account_number":1,"siteUsesCo":"$situses.coordinates"}},{"$out" : "forcsv"})

And then export from forcsv collection like-

mongoexport --db test --collection forcsv --csv --fields 'account_number,siteUsesCo'  --out coordinates.csv

And after exporting delete collection forcsv.



回答2:

It looks like this is a known bug to be fixed in 3.0.5.
See this; https://jira.mongodb.org/browse/TOOLS-657



回答3:

And one more solution, where you can configure output in flexible way

mongo host:port/test --quiet query.js -u username -p passw0rd > accounts.csv

and query.js:

db = db.getSiblingDB('test');

db.getCollection('accounts').find({}, {account_number:1, situses:1, _id:0}).forEach(
    function(item_data) { print(`${item_data.account_number},${item_data.situses[0].coordinates}`); });


标签: mongodb