Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export one object with mongoexport, how to specify _id?

Tags:

mongodb

I'm trying to export just one object with mongoexport, filtering by its ID.

I tried:

mongoexport -d "kb_development" -c "articles" -q "{'_id': '4e3ca3bc38c4f10adf000002'}"

and many variations, but it keeps saying

connected to: 127.0.0.1
exported 0 records

(and I'm sure there is such an object in the collection)

In mongo shell I would use ObjectId('4e3ca3bc38c4f10adf000002'), but it does not seem to work in the mongoexport query.

like image 820
Blacksad Avatar asked Aug 23 '11 01:08

Blacksad


2 Answers

I think you should be able to use ObjectId(...) in the query argument to mongoexport:

mongoexport -d kb_development -c articles -q '{_id: ObjectId("4e3ca3bc38c4f10adf000002")}'

If that does not work, you can use the "strict mode" javascript notation of ObjectIds, as documented here:

mongoexport -d kb_development -c articles -q '{_id: {"$oid": "4e3ca3bc38c4f10adf000002"}}'

(Also note that strict mode JSON is the format produced by mongoexport)

like image 168
dcrosta Avatar answered Sep 18 '22 20:09

dcrosta


You have to specify the _id field by using the ObjectId type. In your question it was specified as a string.

CODE ::

mongoexport -h localhost -d my_database -c sample_collection -q '{key:ObjectId("50584580ff0f089602000155")}' -o my_output_file.json

NOTE :: dont forgot quotes in query

like image 25
Harikrushna Adiecha Avatar answered Sep 18 '22 20:09

Harikrushna Adiecha