将MongoDB查询转换为mongoexport

时间:2014-07-15 17:02:46

标签: mongodb mongoexport

我有以下Mongo查询

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})

我尝试将结果导出为CSV。 mongoexport能够进行这样的查询吗?任何帮助表示赞赏。

使用MongoDB 2.2.3版

1 个答案:

答案 0 :(得分:3)

Mongoexport 2.2.x版可以做一些 - 查询和排序。它不支持限制 - 版本2.6支持该选项。

查询作为json文档传递给mongoexport:

  

- 查询

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

您可以通过$ orderby排序(未记录,但这里有一个很好的讨论):

How to export sorted data using mongoexport?

和示例:

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

版本2.6之前不支持限制:

  

- 限制

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

2.6也有不同的排序语法:

  

- 排序

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

超过32兆字节。

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

链接:

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