Node. js example of mongoDB operations _ node. js

Source: Internet
Author: User
Here we will share with you node. examples of js operations on the mongoDB database, including connecting to the database, inserting data, closing the database, reading data, and inserting data, are very comprehensive. We recommend this to our partners. Connect to database

The Code is as follows:


Var mongo = require ("mongodb ");
Var host = "localhost ";
Var port = mongo. Connection. DEFAULT_PORT;
Var server = new mongo. Server (host, port, {auto_reconnect: true}); // the server on which the database is created
Var db = new mongo. Db ("node-mongo-examples", server, {safe: true}); // create a database object
Db. open (function (err, db) {// connect to the database
If (err)
Throw err;
Else {
Console. log ("database connection established successfully ");
Db. close ();
}
});
Db. on ("close", function (err, db) {// close the database
If (err) throw err;
Else console. log ("database closed successfully .");
});

Insert data:

After inserting data, output the content of the data document in the console

The Code is as follows:


Var mongo = require ("mongodb ");
Var host = "localhost ";
Var port = mongo. Connection. DEFAULT_PORT;
Var server = new mongo. Server (host, port, {auto_reconnect: true}); // the server on which the database is created
Var db = new mongo. Db ("node-mongo-examples", server, {safe: true}); // create a database object
Db. open (function (err, db) {// connect to the database
If (err)
Throw err;
Else {
Db. collection ("users", function (err, collection ){
Collection. insert ({username: "panpan", firstname: "Li"}, function (err, docs ){
Console. log (docs );
Db. close ();
});
});
}
});
Db. on ("close", function (err, db) {// close the database
If (err) throw err;
Else console. log ("database closed successfully .");
});

Close DatabaseDb. close ([forceClose], [callback]);

When forceClose is true, the database is forcibly disabled. When the database is closed, you cannot use open to enable the database.

When forceClose is set to false, the database is not forcibly closed. After the database is closed, open the database again.

When foreClose is true:

The Code is as follows:


Var mongo = require ("mongodb ");
Var host = "localhost ";
Var port = mongo. Connection. DEFAULT_PORT;
Var server = new mongo. Server (host, port, {auto_reconnect: true}); // the server on which the database is created
Var db = new mongo. Db ("node-mongo-examples", server, {safe: true}); // create a database object
Db. open (function (err, db) {// connect to the database
If (err)
Throw err;
Else {
Db. collection ("users", function (err, collection ){
Collection. insert ({username: "panpan", firstname: "Li"}, function (err, docs ){
Console. log (docs );
Db. close (false );
});
});
}
});
Db. once ("close", function (err, db) {// close the database
If (err) throw err;
Else {
Db. open (function (err, db ){
Db. collection ("users", function (err, collection ){
Collection. insert ({username: "3", firstname: "Zhang"}, function (err, docs ){
If (err) throw err;
Else {
Console. log (docs );
Db. close (true );
}
})
});
});
}
});

//Read data

The Code is as follows:


Var mongo = require ("mongodb ");
Var host = "localhost ";
Var port = mongo. Connection. DEFAULT_PORT;
Var server = mongo. Server (host, port, {auto_reconnect: true });
Var db = new mongo. Db ("node-mongo-examples", server, {safe: true });
Db. open (function (err, db ){
Db. collection ("users", function (err, collection ){
If (err) throw err;
Else {
Collection. find ({}). toArray (function (err, docs ){
If (err) throw err;
Else {
Console. log (docs );
Db. close ();
}
});
}
});
});

// Search with query Conditions

The Code is as follows:


Var mongo = require ("mongodb ");
Var host = "localhost ";
Var port = mongo. Connection. DEFAULT_PORT;
Var server = mongo. Server (host, port, {auto_reconnect: true });
Var db = new mongo. Db ("node-mongo-examples", server, {safe: true });
Db. open (function (err, db ){
Db. collection ("users", function (err, collection ){
If (err) throw err;
Else {
Collection. find ({username: {$ in: ["Yansi", "3"]}). toArray (function (err, docs ){
If (err) throw err;
Else {
Console. log (docs );
Db. close ();
}
});
}
});
});

//Insert a batch of dataAnd search for type = food and the price field value is less than 10

The Code is as follows:


Var mongo = require ("mongodb ");
Var host = "localhost ";
Var port = mongo. Connection. DEFAULT_PORT;
Var server = mongo. Server (host, port, {auto_reconnect: true });
Var db = new mongo. Db ("node-mongo-examples", server, {safe: true });
Var docs = [
{Type: "food", price: 11 },
{Type: "food", price: 10 },
{Type: "food", price: 9 },
{Type: "food", price: 8 },
{Type: "book", price: 9}
];
Db. open (function (err, db ){
Db. collection ("goods", function (err, collection ){
If (err) throw err;
Else {
Collection. insert (docs, function (err, docs ){
If (err) throw err;
Else {
Collection. find ({type: "food", price: {$ lt: 10}). toArray (
Function (err, docs ){
If (err) throw err;
Else {
Console. log (docs );
Db. close ();
}
}
);
}
})
}
});
});

Expression of or in the query:

The Code is as follows:


Collection. find ({$ or :[
{Type: "food "},
{Price :{$ lt: 10 }}
]})

About node. the introduction of js operations on mongoDB databases will come here today. Basically, common operations are provided with examples. If it is more complicated, let's take full advantage of it. We will continue to explain it if we have the opportunity.

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.