Current location - Loan Platform Complete Network - Big data management - Front-end how to request nodejs written interface operation mongodb
Front-end how to request nodejs written interface operation mongodb

The first thing to do is to configure the download, download node, enter node -v can see the version number even if you have installed node, my version is v4.4.3, is also an old version.

Then the mongodb download. Just go directly to the official website and download it.

After downloading, you can always confirm and install by default.

After installation, you can see the path. C:\Program Files\MongoDB\Server\3.2\bin , my computer defaults to this path, the files under this path contain the server mongod.exe, server mongo.exe.

Enter and run.

Running the client

Enter and run to see.

Then show dbs can see the existing default created local and test.

This time mongodb database is even simulated well. Then it's time to edit the nodejs code to link to the mongodb database.

npm install mongodb?

1 var mongo = require('mongodb'), 2 Server = mongo.Server, 3 Db = mongo. 27017, {auto_reconnect: true}); 6 var db = new Db('foo', server); 7 ?8 db.open(function(err, db) { 9 if(!err) {10 console.log("We are connected");11 } 12 });

Running node code with the node interpreter.

We can see the output we are connected?

The following output from the server means that the connection was successful.

Two connections are open.

There is also a connection through the mongoose module.

npm install mongoose

The nodejs code is as follows.

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test'); //connecting to a test database

On the server side, you can also see a connection being open. Okay, that's it for the initial nodejs connection. There are still a lot of pitfalls to work out for yourself.