Using MongoDB in node. js

Source: Internet
Author: User
Tags install mongodb

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.