简体   繁体   English

MongoDB“eval”执行顺序

[英]MongoDB “eval” order of execution

How using MongoDB shell define function and use it? 如何使用MongoDB shell定义函数并使用它?

In a script file createusers.js there is following code to create a user with read role on specific database. 在脚本文件createusers.js有以下代码用于创建在特定数据库上具有读取角色的用户。

function createReader(database, username, password)
{
db.getSiblingDB(database).createUser({
    user  : username,
    pwd   : password,
    roles : [ { role : "read", db : database } ]
});
}

Is there possibility to execute this function in mongodb shell? 是否有可能在mongodb shell中执行此功能? The following call doesn't succeed 以下调用不成功

mongo --eval="createReader('somedb', 'user1', 'pass1')" createusers.js

gives error createReader is not defined . 给出错误createReader没有定义

您可以使用load()eval加载js文件:

mongo --eval "load('createusers.js'); createReader('somedb', 'user1', 'pass1')"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM