0

Hi i connect my mongodb with command prompt on windows. But i could not with express application. I read too many blog post about node,express and mongodb but could not find the problem. So i need advise.

My database folder : c:/data/db My mongodb folder: c:/mongodb

In command prompt i type:cd bin mongod Then i type: mongo and it says : enter image description here

And i can do whatever i want with command prompt.

But when i try to connect mongodb from my node express application i fail always. My code is shown below:

var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:51241/ecmarketing');
var ObjectID = require('mongodb').ObjectID;
var collection = db.usercollection.findOne();
res.render('index', { title: 'index' });
};

Always i get:

enter image description here

package.json:

{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.4.4",
"ejs": "~0.8.4",
"ejs-locals": "~1.0.2",
"mongodb": "~1.3.19",
"monk": "~0.7.1"
}
}

I installed package with npm install. Also i created ecmarketing file in db folder. Thats all.

4

1 回答 1

1

几点评论:

  • 你确定你的 MongoDB 在端口 51241 上运行吗?正常端口是27017;
  • 使用monk,您需要显式获取对集合的引用:

    var collection = db.get('usercollection');
    
  • 您不必自己为数据库创建文件夹,MongoDB 会为您完成;

于 2013-11-15T12:28:19.560 回答