MongoExport With Query..

244 views
Skip to first unread message

Murthy Jn

unread,
Jun 5, 2016, 9:28:09 AM6/5/16
to mongodb-user
Hello,

I am trying to export few records using Mongexport option. I have successfully exported without query option.

but not able to export the records using query. please help me and let me know the mistake what i did in the below screen.




"mongoexport -d local -c Audit -q "{ db.Audit.find({inserted:{$gte:ISODate("2016-06-04T15:45:00")}})}" -o c:\Users\Murthy\Desktop\output2.json"

Let me know what is the correct Query for this ::

Thanks,
Murthy

Auto Generated Inline Image 1

Pooja Gupta

unread,
Jun 16, 2016, 2:13:22 AM6/16/16
to mongodb-user

Hi Murthy,

I tried to replicate your problem using a dataset similar to your use case. I am able to export the records using the below command:

mongoexport -d community -c free103901 -q '{inserted:{$lte:ISODate("2016-06-13T06:39:38.115Z")}}' -o ~/Desktop/test1.json

My Sample Dataset:

> db.free103901.find()
{ "_id" : ObjectId("575e54fb381acd5180ca7127"), "name" : "abc", "inserted" : ISODate("2016-06-13T06:38:51.683Z") }
{ "_id" : ObjectId("575e5519381acd5180ca7128"), "name" : "def", "inserted" : ISODate("2016-06-13T06:39:21.810Z") }
{ "_id" : ObjectId("575e552a381acd5180ca7129"), "name" : "ghi", "inserted" : ISODate("2016-06-13T06:39:38.115Z") }

Query Result:

mongoexport -d community -c free103901 -q '{inserted:{$lte:ISODate("2016-06-13T06:39:38.115Z")}}' -o ~/Desktop/test1.json
2016-06-16T11:01:02.328+0530    connected to: localhost
2016-06-16T11:01:02.329+0530    exported 3 records

test1.json contents:

{"_id":{"$oid":"575e54fb381acd5180ca7127"},"name":"abc","inserted":{"$date":"2016-06-13T06:38:51.683Z"}
{"_id":{"$oid":"575e5519381acd5180ca7128"},"name":"def","inserted":{"$date":"2016-06-13T06:39:21.810Z"}}
{"_id":{"$oid":"575e552a381acd5180ca7129"},"name":"ghi","inserted":{"$date":"2016-06-13T06:39:38.115Z"}}

Please note that you were trying to use mongo shell syntax with mongoexport command while querying : db.Audit.find(...) . Instead you just have to provide a JSON document as a query.

Please see mongoexport for more information on syntaxes and options to use with mongoexport command.

Regards,

Pooja

Murthy Jn

unread,
Jun 16, 2016, 2:52:35 AM6/16/16
to mongodb-user
Hi Pooja,

Thanks for your reply....

As suggested I have tried both ways, but failed to export.

Even I have tried to export your sample document. but still can't see the example below.



Could you please let me know the other possibilities..

Thanks,
Murthy.
export.JPG
Auto Generated Inline Image 1

Pooja Gupta

unread,
Jul 8, 2016, 9:03:24 AM7/8/16
to mongodb-user

Hi Murthy,

Apologies for the delay in response. I was out of office for a few weeks.

Could you please try and interchange single and double quotes in the query:

From

mongoexport..... -q '{inserted:{$lte:ISODate("2016-06-13T06:39:38.115Z")}}' -o ......

To

mongoexport..... -q "{inserted:{$lte:ISODate('2016-06-13T06:39:38.115Z')}}" -o ......

This is because, unlike UNIX, windows doesn’t recognise single quotes as delimiter.

Regards,

Pooja

Reply all
Reply to author
Forward
0 new messages