这里的第一个问题,所以要友善;)
我正在配置一个Node.js服务器以连接到Modulus.io node.js主机中的MongoDB数据库(非常好的东西,值得一试),但是我似乎不能正确地建立连接。Per the getting-started guide我得到一个连接uri,格式为:
mongodb://user:pass@mongo.onmodulus.net:27017/3xam913
但这似乎不适用于我试图移植到服务器(让它在本地运行)的代码结构,因为Server class参数结构只有主机和端口要定义……
这是我试图适应这个连接的代码:
// server setup
var mongo = require('mongodb'),
mdbServer = mongo.Server,
mdbDb = mongo.Db,
mdbObjectID = mongo.ObjectID;
// open a connection to the mongoDB server
var mdbserver = new mdbServer('localhost', 27017, {auto_reconnect: true});
// request or create a database called "spots03"
var db = new mdbDb('spots03', mdbserver, {safe: true});
// global var that will hold the spots collection
var spotsCol = null;
// open the database
db.open(function(err, db) {
if(!err) {
// if all cool
console.log("Database connection successful");
// open (get/create) a collection named spotsCollection, and if 200,
// point it to the global spotsCol
db.createCollection(
'spotsCollection',
{safe: false}, // if col exists, get the existing one
function(err, collection) {spotsCol = collection;}
);
}
});任何帮助都将不胜感激,谢谢!
发布于 2012-12-30 07:48:41
看起来像是几件事:
var mdbserver = new mdbServer('mongo.onmodulus.net',27017,{auto_reconnect: true});
true});
我是一个Modulus开发人员,我知道DB名称的事情并不理想。
编辑:这里是一个工作示例的完整源代码。它记录每个HTTP请求,然后将所有请求发送回用户。
var express = require('express'),
mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db;
var app = express();
var server = new Server('mongo.onmodulus.net', 27017, { auto_reconnect: true });
var client = new Db('piri3niR', server, { w: 0 });
client.open(function(err, result) {
client.authenticate('MyUser', 'MyPass', function(err, result) {
if(!err) {
console.log('Mongo Authenticated. Starting Server on port ' + (process.env.PORT || 8080));
app.listen(process.env.PORT || 8080);
}
else {
console.log(err);
}
});
});
app.get('/*', function(req, res) {
client.collection('hits', function(err, collection) {
collection.save({ hit: req.url });
// Wait a second then print all hits.
setTimeout(function() {
collection.find(function(err, cursor) {
cursor.toArray(function(err, results) {
res.send(results);
});
});
}, 1000)
});
});发布于 2012-12-29 10:59:46
可能是错误的数据库名称?
在MongoDB docs on the subject中,'3xam913‘是您的数据库名称,而不是'spots03’。
var db = new mdbDb('3xam913', mdbserver, {safe: true});https://stackoverflow.com/questions/14072603
复制相似问题