Mongo s cli 告诉您自己如何使用 js 文件
$ mongo --help
MongoDB shell version: 2.0.3
usage: mongo [options] [db address] [file names (ending in .js)]
...
<强 > usage:mongo [选项][db地址] [文件名称(以.js结尾)] 强>
例如:
$ echo db.addUser("guest", "passwordForGuest", true); > file.js
$ mongo mydb file.js
MongoDB shell version: 2.0.3
connecting to: mydb
{ "n" : 0, "connectionId" : 1, "err" : null, "ok" : 1 }
{
"user" : "guest",
"readOnly" : true,
"pwd" : "b90ba46d452e5b5ecec64cb64ac5fd90",
"_id" : ObjectId("4fbea2b013aacb728754fe10")
}
Udpate:
db.addUser deprecated since 2.6
https://docs.mongodb.com/v2.6/reference/method/db.addUser/
使用 db. createUser
代替:
// file.js
db.createUser(
{
user: "guest",
pwd: "passwordForGuest",
roles: [ { role: "read", db: "mydb" } ]
}
)
$mongo mydb 文件.js