这只是一个简单的检查,如果集合不存在,则创建它并预填充数据。这在我的本地机器上工作正常,并且取决于在 db.collection 调用上设置的“严格”标志,如果 coll 不存在,它将引发错误。然而,它在 Heroku 上不起作用,有人知道为什么吗?
mongo.Db.connect(mongoUri, function (err, db) {
if (!err)
{
console.log("Connected to Database")
db.collection('collectionName', {strict:true}, function(er, collection) {
if (!er)
{
console.log("Collection opened, ready for use!")
} else {
console.log("DATA DOES NOT EXIST, PREPOPULATING")
populateDB();
}
});
} else {
console.log("Error connecting to database: " + err)
}
});
包.json
{
"name": "appname",
"description": "Application",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.x",
"mongodb": "1.3.x",
"socket.io": "0.9.10"
},
"engines": {
"node": "0.8.4",
"npm": "1.1.49"
}
}