6

I read :

And I am really confused. How I should work with mongoDB from node.js? I’m a rookie, and my question may look stupid.

var db = new db.MongoClient(new db.Server('localhost', 27017));
    db.open(function(err, dataBase) {
       //all code here?
       dataBase.close();
    });

Or every time when I needing something from db I need call:

MongoClient.connect("mongodb://localhost:27017/myDB", function(err, dataBase) {
    //all code here 
    dataBase.close();
});

What is the difference betwen open and connect? I read in the manual that open: Initialize and second connect. But what exactly does that mean? I assume that both do the same, but in the other way, so when should I use one instead the other?

I also wanna ask it's normal that mongoClient needing 4 socket? I running two myWEbServer at the same time, here’s picture: http://i43.tinypic.com/29mlr14.png

EDIT: I wanna mention that this isn't a problem ( rather doubt :D), my server works perfect. I ask because I wanna know if I am using mongoDB driver correctly. Now/Actually I use first option,init mongo dirver at the beginning and inside load put all code.

Community
  • 1
  • 1
Sonny D
  • 897
  • 9
  • 29
  • 2
    The 4 is connection pooling – Sammaye Sep 24 '13 at 20:55
  • Is it not possible to use Mongoose? http://mongoosejs.com/ Much simpler in my opinion. – Tony Sep 24 '13 at 21:00
  • Have you looked at this: https://github.com/mongodb/node-mongodb-native#introduction? – Tony Sep 24 '13 at 21:07
  • @ Sammaye – I got it, thanks. @Tony – I knew that, someone will suggest some library.I read about it, I can use it, but I won't. Mongoose make it simpler, faster, better… maybe. But I don't need it as soon as possible. I write it to myself. Also I don't like abstraction layer. I study so I wanna two thing: simple and basic ( using core funtion which provide driver) code which I understand. It may sound stupid for you(everyone), if so just ignore my question. – Sonny D Sep 24 '13 at 22:24
  • you can find a nodejs and mongodb tutorial. https://programmerblog.net/nodejs-mongodb-tutorial/ – Maz I Sep 06 '17 at 08:35

3 Answers3

2

I'd recommend trying the MongoDB tutorial they offer. I was in the same boat, but this breaks it down nicely. In addition, there's this article on github that explains the basics of DB connection.

In short, it does look like you're doing it right.

MongoClient.connect("mongodb://localhost:27017/myDB", function(err, dataBase) {
    //all code here 
    var collection = dataBase.collection('users');
    var document1 = {'name':'John Doe'};
    collection.insert(document1, {w:1}, function(err,result){
        console.log(err);
    });
    dataBase.close();
});
Brandon Anzaldi
  • 6,884
  • 3
  • 36
  • 55
  • I read this. I know how to work with mongoDB, from main tutorial. For every drivers querying rule are the same. What you suggest is wrong, look: http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html I wrote above guessing about difference between open and connect method. Why you have two functions doing exactly the same? This would not be a problem if someone write next to one ‘deprecated’ – Sonny D Sep 25 '13 at 08:43
1

You still can sign up for a free course M101JS: MongoDB for Node.js Developers, provided by MongoDB guys

Here is short description:

This course will go over basic installation, JSON, schema design, querying, insertion of data, indexing and working with language drivers. In the course, you will build a blogging platform, backed by MongoDB. Our code examples will be in Node.js.

Andrzej Karpuszonak
  • 8,896
  • 2
  • 38
  • 50
1

I had same question. I couldn't find any proper answer from mongo documentation. All document say is to prefer new db connection and then use open (rather than using connect() ) http://docs.mongodb.org/manual/reference/method/connect/

Trimal
  • 181
  • 1
  • 3