我编写了一个脚本,它接受了一些变量并相应地更新了我的数据库。每次我尝试在命令行中运行脚本时,我似乎都会连接到错误的数据库:
conn = new Mongo();
db = conn.getDB("my_db");
db.auth({
user:DB_USER,
password: DB_PASSWORD
});
cust = db.customers;
print('===== Indexes before execution =====');
print(JSON.stringify(db.customers.getIndexes()));
cust.createIndex({geo_point:"2dsphere"});
print('===== Indexes after execution =====');
print(JSON.stringify(db.customers.getIndexes()));
这就是我运行它的方式:mongo --eval 'let DB_USER="usr", DB_PASSWORD="pwd"' script.js
这是输出:MongoDB shell version: 3.2.16
connecting to: test
2017-09-20T22:51:08.589+0000 E QUERY [thread1] SyntaxError: missing ; before statement @(shell eval):1:4
正如您所看到的那样,“连接到测试”。不确定那是什么,也不确定missing ; before statement @(shell eval):1:4
请注意,当我在shell中运行mongo
时,我得到了
MongoDB shell version: 3.2.16
connecting to: test
非常感谢所有帮助。谢谢!