Search code examples
mongodbmongodb-querymongoexport

MongoExport using a query


when i try to get data out from mongoDB. i am able to show what i want to achieve using this query

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

where the result is

[ "Who else loves Apple ?" ]

Additionally this is how, my document looks like

{
        "_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"
        }
}

But when i tried to export it out, with this sentence below. I am experiencing error

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

Where the error message is

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

May i ask, is it possible to export out what i want to achieve like how i can show in the mongodb?


Solution

  • This part of your mongoexport command ...

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

    ... suggests that you are trying to specify the filter and projections by using the query parameter. According to the docs the query parameter:

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

    So, that's only for the filter.

    You can use the fields parameter to limit the output to specific field(s). From the docs:

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

    So, your mongoexport command should be:

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

    However, since you have chosen JSON output format the following (from the docs) is relevant:

    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.

    Since you are attempted to select a specific field from a sub document and using JSON output format, mongoexport will include "the sub-document with all its fields, not just the specified field within the document".

    This sounds very like the issue raised in an earlier question from you.