Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

mongodb - mongoexport fields from subdocuments to csv

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...