How to mongoexport with one field

不打扰是莪最后的温柔 提交于 2019-12-06 23:09:27

If this ...

db.sTest.find({}, {'comments_data.message':1, _id:0})

... selects the data you are interested in then the equivalent mongoexport command is:

mongoexport --db sDB --collection sTest --fields 'comments_data.message' --type csv --out test88.csv

Note: this uses --type csv because, according to the docs, use of the JSON output format causes MongoDB to export all fields in the selected sub document ...

For csv output formats, mongoexport includes only the specified field(s), and the specified field(s) can be a field within a sub-document.

For JSON output formats, mongoexport includes only the specified field(s) and the _id field, and if the specified field(s) is a field within a sub-document, the mongoexport includes the sub-document with all its fields, not just the specified field within the document.

If you must have JSON format and limit your output to a single field then I think you'll need to write the reduced documents to a separate collection and export that collection, as per this answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!