6

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.

itsraghz
  • 857
  • 1
  • 11
  • 25
  • 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 – Nikos M. Sep 17 '19 at 15:31
  • Add a sample employee document to the question, please – Caconde Sep 17 '19 at 15:34
  • @Caconde, thanks. I have added the snippet of my employee collection that has got 3 documents. – itsraghz Sep 17 '19 at 15:39
  • @NikosM, thanks. I have been working on that direction as well to resolve the 'cannot marshal value' error. But the same code worked for almost all the fellow batchmates using the same machine and same Mongodb version. I am surprised on the fact though! – itsraghz Sep 17 '19 at 15:40
  • @itsraghz have you tried to change `-q '{"designation":"Developer"}'` to `-q "{'designation':'Developer'}"`? – Caconde Sep 17 '19 at 15:53
  • @Caconde, Thank you. Yes, indeed. But that gave the first error **Failed: error parsing query as Extended JSON: invalid JSON input** – itsraghz Sep 17 '19 at 15:56
  • 4
    Try the same with double quotes inside: `-q "{\"designation\":\"Developer\"}"` – Caconde Sep 17 '19 at 15:59
  • @Caconde, thank you very much. It worked. I have added the same below as a different comment/post as an answer. But still not sure and confused on the reason for this tweak! :( – itsraghz Sep 17 '19 at 16:09
  • 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 – Caconde Sep 17 '19 at 16:13
  • Thanks @Caconde, Yes, but still how it worked for my other batchmates? They simply gave it in single quotes as what the first option was, while still they were all using the Windows machine. Moreover, why is the same rule applies when I specify the JSON query in a file? – itsraghz Sep 17 '19 at 16:25
  • Given all those evidences, I believe the most correct answer is: who knows? lol. I really don't have a clue, but I'm glad you managed to solve your problem. – Caconde Sep 17 '19 at 17:03
  • 1
    @Caconde, that so sweet of you :) Thank you for the good support. I appreciate that. – itsraghz Sep 17 '19 at 18:15

3 Answers3

19

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.

itsraghz
  • 857
  • 1
  • 11
  • 25
  • 1
    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 – Nikos M. Sep 18 '19 at 09:20
  • Hey @itsraghz Thank you Very much for this post i was pulling out my hair at the end of a long day – Peter the Russian Sep 19 '19 at 21:08
  • @PetertheRussian, my pleasure. I am glad indeed that you got the way out for the issue ;) – itsraghz Sep 21 '19 at 10:01
  • @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. – itsraghz Sep 21 '19 at 10:02
  • 1
    Thank you so much @itsraghz this helped me a lot. – Ravinda Lakshan Feb 25 '20 at 03:09
  • @Ravindalakshan, happy to see that you had resolved this issue. Glad indeed. – itsraghz Feb 29 '20 at 13:16
  • For me "{\"designation\":\"Developer\"}" didn't work, but I changed the outer double quotes by single quotes, and it worked: `'{\"designation\":\"Developer\"}'` – Benny Dec 13 '22 at 09:29
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

-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

Rafi Patel
  • 46
  • 3
  • 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](/help/how-to-answer). – Community Jul 26 '22 at 17:31