MongoDB is a general-purpose nosql that has been widely used in many places. Next we look at how to use the MongoDB database in node. js.
1. Environment installation
First from the MongoDB official website download, install, configure the environment variables, to ensure that the Mongod command can be successfully started. The MongoDB module is then installed using NPM install MongoDB.
2. Start the database
We start the database by entering the Mongod command at the command line. MongoDB defaults to port 27017, to prevent other apps from taking up this port, causing the database to fail to start. When the database starts successfully, an OK prompt appears, as shown in:
We can then enter the MONGO command to manipulate the database at the command line.
3. Insert operation
After starting the database, we write the insert operation first.
varMongoDB = require (' MongoDB ');varServer =NewMongodb. Server (' 127.0.0.1 ', 27017, {});varClient =NewMongodb. Db (' MyDatabase ', server, {w:1}); Client.open (function(err) {if(ERR)Throwerr; Client.collection (' Test_insert ',function(Err, collection) {if(ERR)Throwerr; Collection.insert ({"title": "I like Cake", "Body": "It is quite good."}, {safe:true}, function(Err, documents) {if(ERR)Throwerr; //console.log (' Document ID is: ' + documents[0]._id); } ); Console.log (' We're now able to perform queries. '); });});
To verify that the data is actually inserted into the database, we do the following at the command line (the MONGO command must be entered first):
As you can see, the data is actually inserted into the Test_insert collection of the database MyDatabase.
4. Query operation
varMongoDB = require (' MongoDB ');varServer =NewMongodb. Server (' 127.0.0.1 ', 27017, {});varClient =NewMongodb. Db (' MyDatabase ', server, {w:1}); Client.open (function(err) {if(ERR)Throwerr; Client.collection (' Test_insert ',function(Err, collection) {if(ERR)Throwerr; Collection.find ({"title": "I like Cake"}). ToArray (function(err, results) {if(ERR)Throwerr; Console.log (results); } ); });});
Run the above code to get the following results:
As you can see, this is the data we inserted.
5. Update operation
varMongoDB = require (' MongoDB ');varServer =NewMongodb. Server (' 127.0.0.1 ', 27017, {});varClient =NewMongodb. Db (' MyDatabase ', server, {w:1}); Client.open (function(err) {if(ERR)Throwerr; Client.collection (' Test_insert ',function(Err, collection) {if(ERR)Throwerr; Collection.update ({"title": "I like Cake"}, {$set: {"Body": "It's So Bad"}}, {safe:true}, function(err) {if(ERR)Throwerr; } ); });});
Run the above code with the following results:
As you can see, the body of the document has been updated.
6. Delete operation
varMongoDB = require (' MongoDB ');varServer =NewMongodb. Server (' 127.0.0.1 ', 27017, {});varClient =NewMongodb. Db (' MyDatabase ', server, {w:1}); Client.open (function(err) {if(ERR)Throwerr; Client.collection (' Test_insert ',function(Err, collection) {if(ERR)Throwerr; Collection.remove ({"title": "I like Cake"}, {safe:true}, function(err) {if(ERR)Throwerr; } ); });});
After running the code, the results are as follows:
The document was deleted.
Using MongoDB in node. js