我在mongoDB的集合中有几个字段。我已经尝试导出所有内容。看起来像这样
{"_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”。
我试图用这个 mongoexport --db sDB --collection sTest --fields data.comments_data --out test88.json
但是当我检查导出的文件时,它只包含如下内容
{"_id":{"$oid":"5a5ef05dbe83813f55141a51"}}
这不是我所期望的。我只想要类似的东西,"message":"Happy bday..Godbless you..."
但是当我在mongoshell上查询时,db.sTest.find({}, {comments_data:1, _id:0})
我可以大致得到我想要的东西。
如果这 ...
db.sTest.find({}, {'comments_data.message':1, _id:0})
...选择您感兴趣的数据,则等效mongoexport
命令为:
mongoexport --db sDB --collection sTest --fields 'comments_data.message' --type csv --out test88.csv
注意:这--type csv
是因为根据docs,使用JSON输出格式会导致MongoDB导出所选子文档中的所有字段...
对于csv输出格式,mongoexport仅包含指定的字段,并且指定的字段可以是子文档中的字段。
对于JSON输出格式,mongoexport仅包含指定的字段和_id字段,并且如果指定的字段是子文档中的字段,则mongoexport会包括该子文档及其所有字段,而不仅仅是文档中的指定字段。
如果您必须具有JSON格式并将输出限制为单个字段,那么我认为您需要根据此答案将简化的文档写入单独的集合并导出该集合。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句