Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to work with node.js and mongoDB

I read :

  • How do I manage MongoDB connections in a Node.js web application?
  • http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html
  • How can I set up MongoDB on a Node.js server using node-mongodb-native in an EC2 environment?

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.

like image 531
Sonny D Avatar asked Sep 24 '13 20:09

Sonny D


People also ask

How will you connect Nodejs and MongoDB?

To connect a Node. js application to MongoDB, we have to use a library called Mongoose. mongoose. connect("mongodb://localhost:27017/collectionName", { useNewUrlParser: true, useUnifiedTopology: true });

Is MongoDB good for node JS?

Depending on your application, combining Node. js and MongoDB works well most of the time — especially if you use a front-end framework like React (MERN), Angular (MEAN), or Vue (MEVN). In this tutorial, you will learn how to create a CRUD application using Node.

Why do we use MongoDB with node js?

The MongoDB Node. js driver makes using MongoDB with Node. js a seamless experience. The driver automatically maps JavaScript objects to BSON documents, meaning that developers can easily work with their data.


1 Answers

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();
});
like image 99
Brandon Anzaldi Avatar answered Sep 23 '22 18:09

Brandon Anzaldi