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.

Community
  • 1
  • 1
ftdeveloper
  • 1,053
  • 3
  • 26
  • 50

1 Answers1

1

A few comments:

  • are you sure your MongoDB is running on port 51241? The normal port is 27017;
  • with monk, you need to explicitly get a reference to the collection:

    var collection = db.get('usercollection');
    
  • you don't have to create a folder for the database yourself, MongoDB will do that for you;

robertklep
  • 198,204
  • 35
  • 394
  • 381
  • I use default port. I change my query to var collection = db.get('usercollection'); But when i write console.log(collection); console give me strange thing. I only want single or multiple user data. – ftdeveloper Nov 15 '13 at 12:52
  • @fuat you subsequently need to use `find` or `findOne` to query the collection – robertklep Nov 15 '13 at 13:12
  • When i use findOne console give me strange data like: {col: {manager:{driver:[Object, collections:[Object,options:[Object]}, driver: {emitter:[Object], state:1, _dbconn:[Object], db:null, _collections: [Object], ObjectID: [Object], ... ... name:'user', col: vs vs vs – ftdeveloper Nov 15 '13 at 14:00
  • @fuat perhaps you should find some examples on how to actually use the monk module – robertklep Nov 15 '13 at 15:02