将输出重定向到MongoDB中的文本文件

时间:2015-05-13 07:06:19

标签: linux mongodb ubuntu pipe

我想将mongodb命令的输出重定向到文件中,但它无法正常工作。我在网上搜索了很多,但没有一个命令对我有效。

> mongo --quiet 99.99.99.99/db --eval 'printjson(db.productAttribute.distinct('productId'))' > /home/myname/output_query.json

Wed May 13 12:28:58.022 SyntaxError: Unexpected identifier

> mongo --quiet db --eval 'printjson(db.productAttribute.distinct('productId'))' > /home/myname/output_query.json

Wed May 13 12:29:09.896 SyntaxError: Unexpected identifier

命令很简单,我不想把它放到一个单独的.js文件中。另外,我试图从mongodb shell本身执行这个命令。

1 个答案:

答案 0 :(得分:3)

首先,你不应该在mongo shell中运行它。这只能在linux shell中使用,因为{shell}中可以使用mongo可执行文件,但是你试图从mongo shell中调用mongo可执行文件。

其次,在命令中使用双引号。

因此,打开终端窗口并运行以下命令。它会起作用。

$ mongo --quiet 99.99.99.99/db --eval 'printjson(db.productAttribute.distinct("productId"))' > /home/myname/output_query.json