使用查询的 MongoExport

MongoExport using a query

当我尝试从 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 中显示的那样?

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

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

... 建议您尝试使用 query 参数指定过滤器 投影。根据the docsquery参数:

Provides a JSON document as a query that optionally limits the documents returned in the export.

所以,这仅适用于过滤器。

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

Specifies a field or fields to include in the export. Use a comma separated list of fields to specify multiple fields.

因此,您的 mongoexport 命令应该是:

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

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

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 输出格式 select 子文档 的特定字段,mongoexport 将包括 "the sub-document with all its fields, not just the specified field within the document".

这听起来很像 中提出的问题。