Nodejs and mongodb

Source: Internet
Author: User
Tags findone install mongodb mongodb driver

First of all to ensure that the correct installation of mongodb, Installation Reference: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian-or-ubuntu-linux

Then download the mongodb driver of nodejs

Npm install mongodb

Compile a test program:

1 var mongodb = require ('mongodb ');
2 var server = new mongodb. Server ('localhost', 27017, {auto_reconnect: true });
3 var db = new mongodb. Db ('mydb', server, {safe: true });
4 db. open (function (err, db ){
5 if (! Err)
6 {
7 console. log ('connect ');
8} else {
9 console. log (err );
10}
11
12 });

If "connect" is displayed, the connection is successful.

Operations on collection of mongodb

There are two methods to link collection:

Db. collection ('mycoll ', function (err, coll ){});

Db. createCollection ('mycoll ', function (err, coll ){});

The two methods have the second optional parameter {safe: true}. this parameter is used for the first method. If this parameter is added, an error is returned when the collection does not exist, for the second method, an error is reported when the collection exists.

Example:

1 var mongodb = require ('mongodb ');

2 var server = new mongodb. Server ('localhost', 27017, {auto_reconnect: true });
3 var db = new mongodb. Db ('mydb', server, {safe: true });
4 db. open (function (err, db ){
5 if (! Err)
6 {
7 console. log ('connect ');
8 db. collection ('mycoll ', {safe: true}, function (err, collection ){
9 if (err ){
10 console. log (err );
11}
12 });
13
14} else {
15 console. Log (ERR );
16}
17
18 });

Result:

 

Example:

1 var mongodb = require ('mongodb ');

2 var Server = new MongoDB. Server ('localhost', 27017, {auto_reconnect: true });
3 var DB = new MongoDB. dB ('mydb', server, {safe: true });
4 dB. Open (function (ERR, DB ){
5 If (! Err)
6 {
7 console. Log ('connect ');
8 dB. createcollection ('mycoll ', {safe: true}, function (ERR, Collection ){
9 If (ERR ){
10 console. Log (ERR );
11}
12 });
13
14} else {
15 console. Log (ERR );
16}
17
18 });

Result:

 

To delete a collection, use the dropCollection function:

Example:

1 var MongoDB = require ('mongodb ');
2 var server = new mongodb. Server ('localhost', 27017, {auto_reconnect: true });
3 var db = new mongodb. Db ('mydb', server, {safe: true });
4 db. open (function (err, db ){
5 if (! Err)
6 {
7 console. log ('connect ');
8 db. dropCollection ('mycoll ', {safe: true}, function (err, result ){
9 console. log (result );
10 });
11
12} else {
13 console. log (err );
14}
15

16 });

Result:

 

Add, delete, modify, and query collections

Add data to collection using the insert Function

Example:

1 var MongoDB = require ('mongodb ');
2 var Server = new MongoDB. Server ('localhost', 27017, {auto_reconnect: true });
3 var DB = new MongoDB. dB ('mydb', server, {safe: true });
4 dB. Open (function (ERR, DB ){
5 If (! Err)
6 {
7 dB. Collection ('mycoll ', {safe: true}, function (ERR, Collection ){
8 var tmp1 = {Title: 'hello', Number: 1 };
9 collection. insert (tmp1, {safe: true}, function (ERR, result ){
10 console. Log (result );
11 });
12 });
13} else {
14 console. Log (ERR );
15}
16
17 });

Result

 

Update Data:

Example:

 

1 var MongoDB = require ('mongodb ');
2 var Server = new MongoDB. Server ('localhost', 27017, {auto_reconnect: true });
3 var DB = new MongoDB. dB ('mydb', server, {safe: true });
4 dB. Open (function (ERR, DB ){
5 If (! Err)
6 {
7 db. collection ('mycoll ', {safe: true}, function (err, collection ){
8 collection. update ({title: 'hello'}, {$ set: {number: 3 }}, {safe: true}, function (err, result ){
9 console. log (result );
10 });
11
12} else {
13 console. log (err );
14}
15
16 });
17

 

Result:

 

Delete data using the remove Function

Example:

1 var mongodb = require ('mongodb ');
2 var server = new mongodb. Server ('localhost', 27017, {auto_reconnect: true });
3 var db = new mongodb. Db ('mydb', server, {safe: true });
4 db. open (function (err, db ){
5 if (! Err)
6 {
7 db. collection ('mycoll ', {safe: true}, function (err, collection ){
8 collection. remove ({title: 'hello'}, {safe: true}, function (err, result ){
9 console. log (result );
10 });
11
12} else {
13 console. log (err );
14}
15
16 });

Result

 

If remove does not have any parameters, delete all.

The search operation has two methods: find and findOne.

Example:

1 var mongodb = require ('mongodb ');

2 var server = new mongodb. Server ('localhost', 27017, {auto_reconnect: true });
3 var db = new mongodb. Db ('mydb', server, {safe: true });
4 db. open (function (err, db ){
5 if (! Err)
6 {
7 db. collection ('mycoll ', {safe: true}, function (err, collection ){
8 var tmp1 = {Title: 'hello '};
9 var tmp2 = {Title: 'World '};
10 collection. insert ([tmp1, tmp2], {safe: true}, function (ERR, result ){
11 console. Log (result );
12 });
13 collection. Find (). toarray (function (ERR, Docs ){
14 console. Log ('Find ');
15 console. log (docs );
16 });
17 collection. findOne (function (err, doc ){
18 console. log ('finone ');
19 console. log (doc );
20 });
21 });

Result:

 

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.