Connect to the database
The code copy 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 server where the database is created is located
var db=new mongo.Db("node-mongo-examples",server,{safe:true});//Create a database object
db.open(function (err,db) {//Connect the database
if(err)
throw err;
else{
console.log("Successfully established database connection");
db.close();
}
});
db.on("close", function (err,db) {//Close the database
if(err) throw err;
else console.log("Successfully closed the database.");
});
Insert data :
After inserting data, output the contents of the data document in the console.
The code copy 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 server where the database is created is located
var db=new mongo.Db("node-mongo-examples",server,{safe:true});//Create a database object
db.open(function (err,db) {//Connect 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("Successfully closed the database.");
});
Close the database db.close([forceClose],[callback]);
When forceClose is true, the database is forced to be closed. After the database is closed, you cannot use open to open the database again.
When forceClose is false, the database is not forced to be closed. When the database is closed, it can be opened with open again.
When foreClose is true:
The code copy 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 server where the database is created is located
var db=new mongo.Db("node-mongo-examples",server,{safe:true});//Create a database object
db.open(function (err,db) {//Connect 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:"three",firstname:"zhang"}, function (err,docs) {
if(err) throw err;
else{
console.log(docs);
db.close(true);
}
})
});
});
}
});
// Read data
The code copy 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 copy 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","San"]}}).toArray(function(err,docs){
if(err) throw err;
else{
console.log(docs);
db.close();
}
});
}
});
});
// Insert a batch of data and search for type==food and the price field value is less than 10
The code copy 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 copy is as follows:
collection.find({$or:[
{type:"food"},
{price:{$lt:10}}
]})
This is the end of the explanation of node.js operating mongoDB database. Basically, there are examples of commonly used operations. For more complex ones, please freely play it. We will continue to explain if you have the chance.