9

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.

12
  • 1
    Certainly {'designation':'Developer'} is not valid json as no single quotes are valid json. So one would need to use double quotes for strings in the json. Then try to resolve other error (which may be OS-specific not MongoDB specific). Else simply use a file to include the json, not literaly Commented Sep 17, 2019 at 15:31
  • Add a sample employee document to the question, please Commented Sep 17, 2019 at 15:34
  • 5
    Try the same with double quotes inside: -q "{\"designation\":\"Developer\"}" Commented Sep 17, 2019 at 15:59
  • 1
    Apparently that happens because in windows you have to put your query between double quotes. So, you have to add escaped double quotes in the query, to make it a valid JSON Commented Sep 17, 2019 at 16:13
  • 1
    @Caconde, that so sweet of you :) Thank you for the good support. I appreciate that. Commented Sep 17, 2019 at 18:15

3 Answers 3

25

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I think it is not about MongoDB at all but rather OS-specific. You run a windows commandf line and windows has certain rules to include literal strings (which is your original problem), which may be different in other OSs. So either escape double quotes and/or include the json through a file not literaly
Hey @itsraghz Thank you Very much for this post i was pulling out my hair at the end of a long day
@PetertheRussian, my pleasure. I am glad indeed that you got the way out for the issue ;)
@NikosM, yes, that is right. But still the other part of the problem remains a mystery where every batchmate using the same Windows 10 machine were able to get it worked in the single quote delimiter but I wasn't.
@Ravindalakshan, happy to see that you had resolved this issue. Glad indeed.
|
0

There is another approaches that I found out to work which were using the triple double-quote (""") for outside encasing.

mongoexport -d trainingdb -c employee -f empId,name,designation -q """ {"designation":"Developer"} """ -o \mongoexport\developers.json

Comments

-1

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"}". for me it was "{\"sensor_name\":\"Heat Recovery System Header Mass Flow\"}"

THIS ANSWER SOLVED MY ISSUE TYSM

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.