我编写了一个数据库填充脚本(在 JavaScript 中),以便我可以轻松地在本地和服务器上运行它。我想从命令行运行它,所以我所要做的就是将它推送到服务器/设备并执行它。该脚本在我的本地运行良好,但是当我尝试在 OpenShift 设备上运行它时,它会出现以下错误:
MongoDB shell version: 2.4.9
connecting to: 127.XX.XXX.X:27017/admin
Sat Sep 12 12:20:25.979 count failed: { "ok" : 0, "errmsg" : "unauthorized" }
at src/mongo/shell/query.js:180
failed to load: ./Create.js
我正在尝试执行命令:
[xxxxxx-xxxxxx.rhcloud.com scripts]\> mongo ./Create.js
我已经包含了我的文件的内部内容。我遗漏了所有文档创建的东西。只知道我将文档添加到数组中。
// Switch to the DB we want
var connection;
try {
// Try to connect to local first
connection = new Mongo();
}
catch(err) {
// If error, connect to OpenShift server
connection = new Mongo('127.XX.XXX.X:27017');
}
db = connection.getDB('xxxxxxx');
// Create array to store documents
var newDocs = [];
...
// Documents are added to newDocs here
...
// If any new documents exist, insert them
if(newDocs.length) {
var bulk = db.xxxxxxx.initializeUnorderedBulkOp();
newDocs.forEach(function (doc) {
bulk.insert(doc);
});
bulk.execute();
print('NEW DOCS INSERTED');
}
else {
print('NO NEW DOCS');
}