In the previous article we introduced the installation and configuration of MongoDB, and then we'll look at how to manipulate the MongoDB database in node.
Before operating the database, you should first build a database like a relational database to ...
Start the database
Using the command prompt:
1. Before creating a database, we should start the database server
Mongod--dbpath D:\MongoDB\data
Note: Do not close this database server ... Or the database will be shut down.
2. Create a database using the following statement
Use MyDB
This way MongoDB will help us to create them automatically, of course, this time the database is still empty.
3, the test database is not already created
Show DBS
You'll see in the console that the mydb you just created has been created.
You can also see the results of the creation by adding the data file mentioned above.
The above operations on the MONGODB database have been created. The next step is to work with node.
Build Node Project
Create a new empty folder on any disk in this machine, I'm using IntelliJ idea to create a new node project in the IDE.
Before you start writing code, you need to use NPM to download the MongoDB module we used in node, just like a MySQL database driver, so we can connect to the database.
In the Command Line window, CD to the newly created node project. Enter the following code:
NPM Install MongoDB
You will see that there is a node_modules file in the project that contains the MongoDB module.
After all the purchases have been made, we will begin to work on specific coding.
Connecting to a database
Enter the following code to connect to the MyDB database we created above:
/** * Created with IntelliJ idea. * User:administrator * date:15-6-4 * Time: PM 4:18 * To change this template use File | Settings | File Templates. */varMONGO = require (' MongoDB ');varhost = ' localhost ';varPort = 27017;varServer =NewMongo. Server (host,port,{auto_reconnect:true});vardb =NewMongo. Db (' MyDB ', server,{salf:true});d B.open (function(err,db) {if(err) {Throwerr; }Else{Console.log (' Successfully connected database '); Db.close (); }});d B.on (' Close ',function(err,db) {if(err) {Throwerr; }Else{Console.log ("Successfully closed database"); }});
Run the code and you will see two output in the control band:
Successful connection to database shut down database successfully
So the MONOGDB database is connected, and then look at inserting a data into the database.
Insert Database
Enter the following code:
/** * Created with IntelliJ idea. * User:administrator * date:15-6-4 * Time: PM 4:18 * To change this template use File | Settings | File Templates. */varMONGO = require (' MongoDB ');varhost = ' localhost ';varPort = 27017;//default port number for MongoDB databasevarServer =NewMongo. Server (host,port,{auto_reconnect:true});vardb =NewMongo. Db (' MyDB ', server,{salf:true});d B.open (function(err,db) {if(err) {Throwerr; }Else{db.collection (' User ',function(err,collection) {Collection.insert ({username:' Liwei ', age:25,sex: ' Male '},function(Err,docs) {console.log (docs); Output the content we inserted db.close (); }); }); }});d B.on (' Close ',function(err,db) {if(err) {Throwerr; }Else{Console.log ("Successfully closed database"); }});
Run the code in the control you will see as we insert the following:
Also on the command line we can see the data we have inserted:
Node Operation MongoDB Database Insert