Time 2016-06-2613:05:16
In the front-end study also has a period of time, studied Html,css,javascript,jqery,ajax,php,mysql, learned these, learned some fur, also did not have any time to study other technologies, in the Web front-end technology rapid development, Not only to learn the technology of the older generation, the basic html,css, the core of JavaScript ..., to learn the framework of a large pile of memory is already not suitable for the study now ... You don't run far away. Now say Nodejs linked to MongoDB.
These days have been learning the basics of mongdb, followed by the footsteps of the online God (code) to simulate the link MongoDB database, but many problems, the simple steps on the web to simulate on their own to have a lot of problems, and eventually have to solve their own.
First to configure the download, download node, input node-v can see the version number even if it is installed node, my version is v4.4.3, is also an old version.
Then is the download of MongoDB. Just go to the official website to download the good. https://www.mongodb.com/, there are various versions to choose from
After the download is OK and the default installation is good.
If you install it, you'll see the path. C:\Program Files\mongodb\server\3.2\bin, My Computer default is this path, the file under this path contains the server side Mongod.exe, Server Mongo.exe.
Enter run.
When you run the client
Enter run to see.
Then show DBS can see the existing default created by local and test.
This time the MongoDB database is even good for simulation. Then edit the Nodejs code to link to the MongoDB database.
NPM Install MongoDB
1 varMONGO = require (' MongoDB '),2Server =MONGO. Server,3Db =MONGO. Db;4 5 varServer =NewServer (' localhost ', 27017, {auto_reconnect:true});6 vardb =NewDb (' foo ', server);7 8Db.open (function(err, db) {9 if(!err) {TenConsole.log ("We are connected"); One } A});
Use the node interpreter to run the node code.
Can see output we are connected
The following output is visible from the server to indicate that the connection was successful.
Two connections are already open.
There is also the connection via the Mongoose module.
NPM Install Mongoose
The Nodejs code is as follows.
var mongoose = require (' Mongoose '); Mongoose.connect (// database connected to a test
You can also see a connection being opened on the server side. Well, Nodejs's first connection is here. There are still a lot of pits to solve themselves ...
Nodejs details of initial link MongoDB