mongoexport - issue with JSON query (extended JSON - Invalid JSON input)

10,155

The following different approach made it work at last - where I had specified the JSON query with the double quotes escaped with the backslash : -q "{\"designation\":\"Developer\"}".

mongoexport -d trainingdb -c employee -f empId,name,designation -q "{\"designation\":\"Developer\"}" -o \mongoexport\developers.json
2019-09-17T21:33:01.642+0530    connected to: mongodb://localhost/
2019-09-17T21:33:01.658+0530    exported 2 records

cat developers.json
{"_id":{"$oid":"5d80d1ae0d4d526a42fd95ad"},"empId":1001.0,"name":"Raghavan","designation":"Developer"}
{"_id":{"$oid":"5d80d1b40d4d526a42fd95af"},"empId":1003.0,"name":"Sathish","designation":"Developer"}

Thank you very much @Caconde. Your suggestion helped.

But I am really not sure why this does not work in my machine alone and the reason for this tweak in the format of the query.

Share:
10,155
itsraghz
Author by

itsraghz

A software engineer by profession but a Lecturer and Poet by passion!

Updated on June 18, 2022

Comments

  • itsraghz
    itsraghz almost 2 years

    I have started learning MongoDB recently. Today the instructor taught us the mongoexport command. While practicing the same, I face a typical issue which none of the other batchmates including the instructor faced. I use MongoDB version 4.2.0 on my Windows 10 machine.

    If I use mongoexport for my collection without any -q parameter to specify any filtering condition, it works fine.

    mongoexport -d trainingdb -c employee -f empId,name,designation -o \mongoexport\all-employees.json
    
    2019-09-17T18:00:30.300+0530    connected to: mongodb://localhost/
    2019-09-17T18:00:30.314+0530    exported 3 records
    

    However, whenever I specify the JSON query as -q (or --query) it gives an error as follows.

    mongoexport -d trainingdb -c employee -f empId,name,designation -q {'designation':'Developer'} -o \mongoexport\developers.json
    
    2019-09-17T18:01:45.381+0530    connected to: mongodb://localhost/
    2019-09-17T18:01:45.390+0530    Failed: error parsing query as Extended JSON: invalid JSON input
    

    The same error persists in all the different flavors I had attempted with for the query.

    -q {'designation':'Developer'}
    --query {'designation':'Developer'}
    -q "{'designation':'Developer'}"
    

    I had even attempted with a different query condition on the 'empId' as -q {'empId':'1001'} But no luck. I keep getting the same error.

    As per one of the suggestions given in the StackOverflow website, I tried with the following option but getting a different error.

      -q '{"designation":"Developer"}'
    

    The error is : 'query '[39 123 101 109 112 73 100 58 49 48 48 49 125 39]' is not valid JSON: json: cannot unmarshal string into Go value of type map[string]interface {}'.

    2019-09-17T20:24:58.878+0530    query '[39 123 101 109 112 73 100 58 49 48 48 49 125 39]' is not valid JSON: json: cannot unmarshal string into Go value of type map[string]interface {}
    2019-09-17T20:24:58.882+0530    try 'mongoexport --help' for more information
    

    I am really not sure what is missing here ? Tried with a bit of Googling and also gone through the official MongoDB documentation of the mongoexport - but no luck.

    The employee collection in my system looks like the follows with 3 documents.

    > db.employee.find().pretty()
    {
            "_id" : ObjectId("5d80d1ae0d4d526a42fd95ad"),
            "empId" : 1001,
            "name" : "Raghavan",
            "designation" : "Developer"
    }
    {
            "_id" : ObjectId("5d80d1b20d4d526a42fd95ae"),
            "empId" : 1002,
            "name" : "Kannan",
            "designation" : "Architect"
    }
    {
            "_id" : ObjectId("5d80d1b40d4d526a42fd95af"),
            "empId" : 1003,
            "name" : "Sathish",
            "designation" : "Developer"
    }
    >
    

    Update

    As suggested by @NikosM, I have saved the query in a .json file (query.json) and tried the same mongoexport command with the new approach. Still, no luck. Same Marshal error.

    cat query.json
    {"designation":"Developer"}
    
    mongoexport -d trainingdb -c employee -f empId,name,designation -q 'query.json' -o \mongoexport\developers.json
    
    2019-09-17T21:16:32.849+0530    query '[39 113 117 101 114 121 46 106 115 111 110 39]' is not valid JSON: json: cannot unmarshal string into Go value of type map[string]interface {}
    2019-09-17T21:16:32.852+0530    try 'mongoexport --help' for more information
    

    Any help on this will be highly appreciated.