Nodejs to the MongoDB database additions and deletions to the operation (reprint)

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

First of all, to ensure proper installation of MongoDB, installation reference: Http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian-or-ubuntu-linux

You can also refer to my other blog post.

Http://www.cnblogs.com/sexintercourse/p/5774310.html

Guide the development of MONGO and NODEJS

Then download the Nodejs MongoDB driver

NPM Install MongoDB

To write 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 finally displayed, it indicates success.

Operation on MongoDB's collection

There are two ways to link collection, respectively:

Db.collection (' Mycoll ', function (Err,coll) {});

Db.createcollection (' Mycoll ', function (Err,coll) {});

The two methods also have a second optional parameter {safe:true}, the role of this parameter for the first method, if added to this parameter, then when the collection does not exist when the error, for the second method, when the collection exists error

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) {
Ten console.log (ERR);
11}
12});
13
}else{
Console.log (ERR);
16}
17
18});

Results:

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) {
Ten console.log (ERR);
11}
12});
13
}else{
Console.log (ERR);
16}
17
18});

Results:

Deleting collection uses 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
}else{
Console.log (ERR);
14}
15

16});

Results:

The collection is checked for additional

Adding 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) {
Ten console.log (Result);
11});
12});
}else{
Console.log (ERR);
15}
16
17});

Results

To update the 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
}else{
Console.log (ERR);
14}
15
16});
17

Results:

Delete the data by 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
}else{
Console.log (ERR);
14}
15
16});

Results

If remove does not have any parameters, delete all.

Find operations, find operations there are two methods one is find, one is 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 '};
Ten Collection.insert ([Tmp1,tmp2],{safe:true},function (Err,result) {
One console.log (result);
12});
Collection.find (). ToArray (function (Err,docs) {
Console.log (' find ');
Console.log (Docs);
16});
Collection.findone (function (Err,doc) {
Console.log (' FindOne ');
Console.log (DOC);
20});
21});

Nodejs to the MongoDB database additions and deletions to the operation (reprint)

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.