MongoExport with Sub-documents

1,485 views
Skip to first unread message

EKTW

unread,
Sep 17, 2012, 11:45:24 AM9/17/12
to mongod...@googlegroups.com
I have a MongoExport that looks like this:

mongoexport -vv -h mongodb -d database-c collection-q“{‘blah.DateCreated’:{‘$gt’:new Date(1345006800000),’$lt’:new Date(1347771600000)}}" -f_id,field1,field2.
0.Price,field2.0.DateCreated,field2.1.Price,field2.1.DateCreated,field2.2.Price,field2.2.DateCreated,field2.3.Price,field2.3.DateCreated,field2.4.Price,field2.4.DateCreated,field2.5.Price,field2.5.DateCreated --csv -o dataexport.csv

Is there any way to change the export so that it will export the sub-document fields for field2 in however many sub-documents exist? Something like:

mongoexport -vv -h mongodb -d database-c collection-q“{‘blah.DateCreated’:{‘$gt’:new Date(1345006800000),’$lt’:new Date(1347771600000)}}" -f_id,field1,field2.?.Price,field2.?.DateCreated --csv -o dataexport.csv 

Jason Rassi

unread,
Sep 18, 2012, 10:11:19 PM9/18/12
to mongod...@googlegroups.com
No, this is not possible.  mongoexport internally executes a standard query against MongoDB, and its "-f" argument is the analogue of the field selector.  See <http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields> for details about the capabilities of the field selector.

Instead, you can export "field2" in its entirety, or write custom code to export your data to CSV format, or (if space permits) you can write code to save this subset of your data in a new collection (and then run mongoexport on that collection).
Reply all
Reply to author
Forward
0 new messages