0

I have the following Mongo query

db.CustUser.find(
{"transactionData": {"$elemMatch": {"createdAt": {"$lte": ISODate("2013-07-15T00:00:00Z")}}}, "emailsSubscribed": true},
{"fname": 1, "email": 1, "_id": 0}).limit(150000).sort({"_id": -1})

I'm trying to export the results to CSV. Is mongoexport capable of such a query? Any help is appreciated.

Using MongoDB version 2.2.3

Brett
  • 721
  • 2
  • 10
  • 20
  • If not, I'll just go ahead and write a script. More/less curious if mongoexport can be used in this situation. – Brett Jul 15 '14 at 17:13

1 Answers1

3

Mongoexport version 2.2.x can do some of this - the query and the sort. It does not support limit - that option is supported in version 2.6.

The query is passed as a json document to mongoexport:

--query

Provides a JSON document as a query that optionally limits the documents returned in the export.

You can sort via a $orderby (undocumented but here's a good discussion on it):

How to export sorted data using mongoexport?

and example:

--query '{ $query: {}, $orderby: {count: -1} }'

Limit is not supported until version 2.6:

--limit

Specifies a maximum number of documents to include in the export. See limit() for information about the underlying operation.

2.6 also has different syntax for sort:

--sort

Specifies an ordering for exported results. If an index does not exist that can support the sort operation, the results must be less

than 32 megabytes.

Use --sort conjunction with --skip and --limit to limit number of exported documents.

Links:

http://docs.mongodb.org/manual/reference/program/mongoexport/ http://docs.mongodb.org/v2.2/reference/mongoexport/

Community
  • 1
  • 1
John Petrone
  • 26,943
  • 6
  • 63
  • 68