将输出重定向到 MongoDB 中的文本文件
Redirecting the output to a text file in MongoDB
我想将 mongodb 命令的输出重定向到一个文件中,但它不起作用。我在网上搜索了很多,但 none 个命令对我有用。
> 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 本身执行此命令。
首先,你不应该在 mongo shell 中 运行 这个。这仅适用于 linux shell,因为 mongo
可执行文件在 linux shell 中可用,但您正试图从内部调用 mongo
可执行文件mongo
shell.
其次,在命令中使用双引号。
所以,打开一个终端window和运行如下。它会起作用。
$ mongo --quiet 99.99.99.99/db --eval 'printjson(db.productAttribute.distinct("productId"))' > /home/myname/output_query.json
我想将 mongodb 命令的输出重定向到一个文件中,但它不起作用。我在网上搜索了很多,但 none 个命令对我有用。
> 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 本身执行此命令。
首先,你不应该在 mongo shell 中 运行 这个。这仅适用于 linux shell,因为 mongo
可执行文件在 linux shell 中可用,但您正试图从内部调用 mongo
可执行文件mongo
shell.
其次,在命令中使用双引号。
所以,打开一个终端window和运行如下。它会起作用。
$ mongo --quiet 99.99.99.99/db --eval 'printjson(db.productAttribute.distinct("productId"))' > /home/myname/output_query.json