需要将mongodb聚合查询结果导出到文件中。我连接到远程服务器,所以我需要像db.myCollection.aggregate([...]).printToFile('C:\Downloads\resultFile.txt')这样的东西,这是可能的吗?
发布于 2019-01-30 07:06:53
在连接字符串中添加引号,并使用printjson()打印JSON对象。请注意,aggregate()返回一个游标,因此您必须遍历它们。如果你不使用forEach,它只返回TOP20。
下面是一行命令。
mongo --quiet "mongodb://junior:SECRETPASSWORD@mongo4:9000,mongo5:9000/WebApp?authSource=admin&replicaSet=rs0&readPreference=secondaryPreferred" --eval 'db.cars.aggregate([...]).forEach(function(doc) { printjson(doc);})' > output.txt采用可读性更好的多行格式
mongo --quiet \
"mongodb://junior:SECRETPASSWORD@mongo4:9000,mongo5:9000/WebApp?authSource=admin&replicaSet=rs0&readPreference=secondaryPreferred" \
--eval 'db.cars.aggregate([...]).forEach(function(doc) \
{ printjson(doc);})' > output.txthttps://stackoverflow.com/questions/54429779
复制相似问题