mongoexport字段从子文档到csv [英] mongoexport fields from subdocuments to csv

查看:118
本文介绍了mongoexport字段从子文档到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  

输出包含account_number,但不包含子文档中的坐标字段. 根据 docs ,这应该可以工作.

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

以下内容将导出整个siteuses子文档,但我只需要一个字段.

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?

我正在运行Mongodb 3.0.4

I'm running Mongodb 3.0.4

其他信息

以下语法适用于Mongodb的早期版本(2.6.x?).注意subdoc.0.fieldname语法.

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.

推荐答案

语法错误.

从mongo 3.0.0版开始,mongoexport删除了--type = csv选项.使用--type = csv选项为输出指定CSV格式.

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

您应该使用:

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

对于嵌套字段,您应该使用:

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

对带有子文档的mongo 3.0进行

您需要使用-

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"})

然后从forcsv集合中导出,例如-

And then export from forcsv collection like-

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

并且在导出删除集合forcsv之后.

And after exporting delete collection forcsv.

这篇关于mongoexport字段从子文档到csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆