首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用node-mongodb native连接Modulus.io?

如何使用node-mongodb native连接Modulus.io?
EN

Stack Overflow用户
提问于 2012-12-29 00:30:56
回答 2查看 999关注 0票数 0

这里的第一个问题,所以要友善;)

我正在配置一个Node.js服务器以连接到Modulus.io node.js主机中的MongoDB数据库(非常好的东西,值得一试),但是我似乎不能正确地建立连接。Per the getting-started guide我得到一个连接uri,格式为:

mongodb://user:pass@mongo.onmodulus.net:27017/3xam913

但这似乎不适用于我试图移植到服务器(让它在本地运行)的代码结构,因为Server class参数结构只有主机和端口要定义……

这是我试图适应这个连接的代码:

代码语言:javascript
复制
// 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;}
        );
    }
});

任何帮助都将不胜感激,谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-30 07:48:41

看起来像是几件事:

  1. 连接URL应为mongo.onmodulus.net

var mdbserver = new mdbServer('mongo.onmodulus.net',27017,{auto_reconnect: true});

  • rounce是正确的,数据库名称由Modulus自动生成。

true});

  • Modulus db = new mdbDb('3xam913',mdbserver,{safe: var数据库将需要身份验证。在调用createCollection之前,您必须调用auth并将在项目仪表板上设置的用户凭据传递给它。

我是一个Modulus开发人员,我知道DB名称的事情并不理想。

编辑:这里是一个工作示例的完整源代码。它记录每个HTTP请求,然后将所有请求发送回用户。

代码语言:javascript
复制
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)
  });
});
票数 3
EN

Stack Overflow用户

发布于 2012-12-29 10:59:46

可能是错误的数据库名称?

在MongoDB docs on the subject中,'3xam913‘是您的数据库名称,而不是'spots03’。

代码语言:javascript
复制
var db = new mdbDb('3xam913', mdbserver, {safe: true});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14072603

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档