如何运行 mongo --eval? [英] How to run mongo --eval?

查看:195
本文介绍了如何运行 mongo --eval?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行 mongo --help 并且它有一个用于评估 javascript 的选项.

I run mongo --help and it has an options for evaluate javascript.

mongo --eval  arg

但是,我还没有找到任何如何使用它的示例.我尝试运行它,但没有成功.有人知道怎么做吗?

However, I haven't found any example of how to use it. I try to run it but with no success. anyone know how to do it?

原因是,我想通过命令行删除一个集合(以便我可以重用代码并将其放入shell脚本).我知道我可以编写另一个 nodejs 并调用 db 来运行它..但不是 20 行代码,如果有一个只有几行并且更快的解决方案,那就太好了.:)

the reason is that, I want to delete a collection through command line ( so that I can reuse the code and put it into shell script). I know I can write another nodejs and call the db to run it.. but instead of 20 lines of code, it would be nice if there is a solution with only few lines and be faster. :)

推荐答案

那就是:

mongo <server>:<port>/<database> --eval "db.<some_collection>.drop();"

或者如果你想在更多行中做一些事情,你可以调用一个文件(必须以 .js 结尾):

Or if you want to do something in more lines you can call a file (must end with .js):

mongo <server>:<port>/<database> some_instructions.js

这篇关于如何运行 mongo --eval?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆