Ann SJ
Ann SJ

Reputation: 51

mongoexport JSON assertion: 10340 Failure parsing JSON string

I'm trying to export CSV file list from mongoDB and save the output file to my directory, which is /home/asaj/. The output file should have the following columns: name, file_name, d_start and d_end. The query should filter data with status equal to "FU" or "FD", and d_end > Dec. 10, 2012.

In mongoDB, the query is working properly. The query below is limited to 1 data output. See query below:

> db.Samples.find({ $or : [ { status : 'FU' }, { status : 'FD'} ], d_end : { $gte : ISODate("2012-12-10T00:00:00.000Z") } }, {_id: 0, name: 1, file_name: 1, d_start: 1, d_end: 1}).limit(1).toArray();
[
    {
            "name" : "sample"
            "file_name" : "sample.jpg",
            "d_end" : ISODate("2012-12-10T05:1:57.879Z"),
            "d_start" : ISODate("2012-12-10T02:31:34.560Z"),
    }

]
>

In CLI, mongoexport command looks like this:

mongoexport -d maindb -c Samples -f "name, file_name, d_start, d_end" -q "{'\$or' : [ { 'status' : 'FU' }, { 'status' : 'FD'} ] , 'd_end' : { '\$gte' : ISODate("2012-12-10T00:00:00.000Z") } }" --csv -o "/home/asaj/currentlist.csv"

But i always ended up with this error:

connected to: 127.0.0.1
Wed Dec 19 16:58:17 Assertion: 10340:Failure parsing JSON string near: , 'd_end
0x5858b2 0x528cb4 0x52902e 0xa9a631 0xa93e4d 0xa97de2 0x31b441ecdd 0x4fd289
mongoexport(_ZN5mongo11msgassertedEiPKc+0x112) [0x5858b2]
mongoexport(_ZN5mongo8fromjsonEPKcPi+0x444) [0x528cb4]
mongoexport(_ZN5mongo8fromjsonERKSs+0xe) [0x52902e]
mongoexport(_ZN6Export3runEv+0x7b1) [0xa9a631]
mongoexport(_ZN5mongo4Tool4mainEiPPc+0x169d) [0xa93e4d]
mongoexport(main+0x32) [0xa97de2]
/lib64/libc.so.6(__libc_start_main+0xfd) [0x31b441ecdd]
mongoexport(__gxx_personality_v0+0x3c9) [0x4fd289]
assertion: 10340 Failure parsing JSON string near: , 'd_end

I'm having error in ", 'd_end' " in mongoexport CLI. I'm not so sure if it is a JSON syntax error because query works on MongoDB. Please help.

Upvotes: 3

Views: 2491

Answers (2)

thaddeusmt
thaddeusmt

Reputation: 15600

I know it has little to do with this question, but the title of this post brought it up in Google so since I was getting the exact same error I'll add an answer. Hopefully it helps someone.

My issue was adding a MongoId query for _id to a mongoexport console command on Windows. Here's the error:

Assertion: 10340:Failure parsing JSON string near: _id

The problem ended up being that I needed to wrap the JSON query in double quotes, and the ObjectId had to be in double quotes (not single!), so I had to escape those quotes. Here's the final query that worked, for future reference:

 mongoexport -u USERNAME -pPASSWORD -d DATABASE -c COLLECTION 
   --query "{_id : ObjectId(\"5148894d98981be01e000011\")}"

Upvotes: 2

Ann SJ
Ann SJ

Reputation: 51

After asking someone knows MongoDB better than me, we found out that the problem is the

ISODate("2012-12-10T00:00:00.000Z")

We found the answer on this question: mongoexport JSON parsing error

To resolve this error, first, we convert it to strtotime:

php > echo strtotime("12/10/2012");
1355126400

Next, multiple strtotime result by 1000. This date will looks like this:

1355126400000

Lastly, change ISODate("2012-12-10T00:00:00.000Z") to new Date(1355126400000) in the mongoexport command.

Now, the CLI mongoexport looks like this and it works:

mongoexport -d maindb -c Samples -f "id,file_name,d_start,d_end" -q "{'\$or' : [ { 'status' : 'FU' }, { 'status' : 'FD'} ] , 'd_end' : { '\$gte' : new  Date(1355126400000) } }" --csv -o "/home/asaj/listupdate.csv"

Note: remove space between each field names in -f or --fields option.

Upvotes: 2

Related Questions