MongoExport使用查询

时间:2018-01-29 12:05:31

标签: mongodb mongodb-query mongoexport

当我尝试从mongoDB中获取数据时。我能够使用此查询显示我想要实现的目标

db.sInsert.distinct(
    "comments_data.comments.data.message", 
    {post_id: {"$eq": "28011986676" }}
)

结果是

[ "Who else loves Apple ?" ]

此外,我的文档看起来像

{
        "_id" : ObjectId("5a43aa19d4b45e362428e2da"),
        "comments_data" : {
                "id" : "28011986676_10155780942281677",
                "comments" : {
                        "paging" : {
                                "cursors" : {
                                        "after" : "WTI5dGJXVnVkRjlqZAFhKemIzSTZAN4TlRBeE5EWXlPQT09",
                                        "before" : "WTI5dGJXVnVk4TVRZAMk56YzZANVFV4TlRBeE5EWXlPQT09"
                                }
                        },
                        "data" : [
                                {
                                        "created_time" : "2018-01-03T21:23:47+0000",
                                        "message" : "Poor customer care service after became the Singtel customer.I did my          re contract they send acknowledgement email confirmation after no followup.I called again and remains no proper response and action extremely worst customer care service.",
                                        "from" : {
                                                "name" : "Sundararaju G",
                                                "id" : "1020391"
                                        },
                                        "id" : "10155780942281677_10155811924116677"
                                }
                        ]
                }
        },
        "post_id" : "28011986676_10155780942281677",
        "post_message" : "\"Singtel TV celebrated our 10th birthday with 10 awesome experiences for our customers! Each of our winners won a trip of a lifetime - from attending the Emmy Awards, getting a magical princess treatment at Disneyland, to catching a Premier League game live in London! We thank all our customers for your support and we look forward to more great years to come!\"",
        "reactions_data" : {
                "reactions" : {
                        "paging" : {
                                "cursors" : {
                                        "after" : "TVRBd01EQXpNVEF5T1Rje4TXc9PQZDZD",
                                        "before" : "TVRjNE56TTBBek56a3hNek14TWc9PQZDZD"
                                },
                                "next" : "https://graph.facebook.com/v2.7/280119866761677/reactions?access_token=EAA"
                        },
                        "data" : [
                                {
                                        "type" : "ANGRY",
                                        "id" : "1020573391",
                                        "name" : "Sundararaju Gh"
                                },
                                {
                                        "type" : "LIKE",
                                        "id" : "64721496",
                                        "name" : "Zhiang Xian"
                                }
                        ]
                },
                "id" : "28011986676_102281677"
        }
}

但是,当我试图将其导出时,请使用以下句子。我遇到了错误

mongoexport --db sDB --collection sInsert --query '{"post_id":{$gte:28011986676}}',{ comments_data.comments.data.message}' --out test.json

错误消息在哪里

2018-01-29T19:56:31.442+0800    too many positional arguments: [comments_data.comments.data.message}']
2018-01-29T19:56:31.526+0800    try 'mongoexport --help' for more information

我可以问,是否有可能将我想要实现的内容输出到mongodb中?

1 个答案:

答案 0 :(得分:2)

你的mongoexport命令的这一部分...

--query '{"post_id":{$gte:28011986676}}',{ comments_data.comments.data.message}'

...建议您尝试使用query参数指定过滤器投影。根据{{​​3}} query参数:

  

提供JSON文档作为查询,可选择限制导出中返回的文档。

所以,那只是过滤器。

您可以使用fields参数将输出限制为特定字段。来自the docs

  

指定要包含在导出中的字段。使用逗号分隔的字段列表指定多个字段。

因此,您的mongoexport命令应为:

mongoexport --db sDB --collection sInsert --query '{"post_id":{$gte:28011986676}}' --fields "comments_data.comments.data.message" --out test.json

但是,由于您选择了JSON输出格式,因此以下(来自文档)是相关的:

  

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

由于您尝试使用JSON输出格式从子文档中选择特定字段,mongoexport将包含"子文档及其所有字段,而不是只是文档中的指定字段"。

这听起来非常像the docs中提出的问题。