执行mongoexport导出CSV返回0条记录,求助排查问题
导致mongoexport导出0条记录的问题排查
下面是你的命令中存在的几个关键问题:
JSON语法错误:
--query参数里有两处语法问题create_time的$lt值后缺失闭合双引号,正确写法应为"$lt": "2023-01-02 00:00"- 因上述引号缺失,导致
create_time的对象未正确闭合,进而整个查询JSON结构损坏
URI参数错误
authsSource拼写错误,正确参数名是authSource- URI末尾缺少闭合双引号,完整URI应为
"mongodb://username:password@localhost1,localhost2/?authSource=admin"
日期查询可能不匹配:如果
create_time字段存储的是MongoDB原生Date类型而非字符串,直接用"2023-01-01 00:00"这类字符串查询会无法匹配数据。此时需要将查询中的日期改为ISODate格式,示例如下:{"program": "online", "create_time": {"$gte": ISODate("2023-01-01T00:00:00Z"), "$lt": ISODate("2023-01-02T00:00:00Z")}, "amount" : {"gt": 0}}注意在shell环境执行时,可能需要对特殊字符转义,或使用
--queryFile参数通过文件指定查询内容来规避转义问题。字段有效性验证:确认
--fields指定的field1,field2,field3确实存在于transaction集合的文档中,若字段不存在,即使查询有结果也会导出空行,无结果时则生成空文件。
修正后的命令示例
mongoexport --db=customer --collection=transaction --type=csv \ --query='{"program": "online", "create_time": {"$gte": "2023-01-01 00:00", "$lt": "2023-01-02 00:00"}, "amount" : {"gt": 0}}' \ --out=out.csv --fields=field1,field2,field3 \ --uri="mongodb://username:password@localhost1,localhost2/?authSource=admin"
内容的提问来源于stack exchange,提问作者Nue
相关产品推荐
相关产品推荐

