如何使用一个字段进行mongoexport [英] How to mongoexport with one field

查看:228
本文介绍了如何使用一个字段进行mongoexport的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mongoDB的集合中有几个字段. 我已经尝试导出所有内容. 看起来像这样

i have a few fields in my collection at the mongoDB. i have tried exported out everything. which looking like this

{"_id":{"$oid":"5a5ef05dbe83813f55141a51"},"comments_data":{"id":"211","comments":{"paging":{"cursors":{"after":"WzZANVFV4TlRVME5qUXpPUT09","before":"WTI5dEF4TlRVNE1USTVNemczTXpZAMk56YzZANVFV4TlRBMU9ERTFNQT09"}},"data":[{"created_time":"2018-01-04T09:29:09+0000","message":"Super","from":{"name":"M Mun","id":"1112"},"id":"1111"},{"created_time":"2018-01-07T22:25:08+0000","message":"Happy bday..Godbless you...","from":{"name":"L1","id":"111"},"id":"1111"},{"created_time":"2018-01-10T00:22:00+0000","message":"Nelson ","from":{"name":"Boon C","id":"1111"},"id":"10111"},{"created_time":"2018-01-10T01:07:19+0000","message":"Thank to SingTel I like to","from":{"name":"Sarkar WI","id":"411653482605703"},"id":"10155812413346677_10155825869201677"}]}},"post_id":"28011986676_10155812413346677","post_message":"\"Usher in the New Year with deals and rewards that will surely perk you up, exclusively for Singtel customers. Find out more at singtel.com/rewards\"",

但是现在我只想导出一个字段,即集合中'comments_data'中的'message'.

but now i want to export just a single field which is the 'message' from the 'comments_data' from the collection.

我尝试使用此mongoexport --db sDB --collection sTest --fields data.comments_data --out test88.json

但是当我检查导出的文件时,它只包含这样的内容

but when i check my exported file, it just contains something like this

{"_id":{"$oid":"5a5ef05dbe83813f55141a51"}}

这不是我所期望的. 我只想要类似"message":"Happy bday..Godbless you..."的东西 但是当我用db.sTest.find({}, {comments_data:1, _id:0})查询在mongoshell上时,我可以大致得到我想要的东西.

which is something not i have expected. i just want something like "message":"Happy bday..Godbless you..." but when i query out at the mongoshell with db.sTest.find({}, {comments_data:1, _id:0})i can roughly get what i want.

推荐答案

如果这...

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

...选择您感兴趣的数据,然后等效的mongoexport命令为:

... 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

注意:使用--type csv是因为,根据 docs ,使用JSON输出格式会导致MongoDB导出所选子文档中的所有字段...

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 ...

对于csv输出格式,mongoexport仅包含指定的字段,并且指定的字段可以是子文档中的字段.

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

对于JSON输出格式,mongoexport仅包含指定的字段和_id字段,并且如果指定的字段是子文档中的字段,则mongoexport包括该子文档及其所有字段,而不仅仅是文档中的指定字段.

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.

如果您必须具有JSON格式并将输出限制为单个字段,那么我认为您需要将精简文档写入单独的集合中,并按照集合="https://stackoverflow.com/a/30906709/8200937">此答案.

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.

这篇关于如何使用一个字段进行mongoexport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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