Connect to database
var mongo=require("mongodb");
var host="localhost";
var port=mongo.Connection.DEFAULT_PORT;
var server=new mongo.Server(host,port,{auto_reconnect:true});//Create the server server where the database is located
var db=new mongo.Db("node-mongo-examples",server,{safe:true});//Create database object
db.open(function (err,db) {//Connect to database
If(err)
throw err;
else{
console.log("Database connection successfully established");
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 contents of the data document in the console
var mongo=require("mongodb");
var host="localhost";
var port=mongo.Connection.DEFAULT_PORT;
var server=new mongo.Server(host,port,{auto_reconnect:true});//Create the server server where the database is located
var db=new mongo.Db("node-mongo-examples",server,{safe:true});//Create database object
db.open(function (err,db) {//Connect to database
If(err)
throw err;
else{
db.collection("users", function (err,collection) {
collection.insert({username:"Panpan", firstname:"李"}, 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 the databasedb.close([forceClose],[callback]);
When forceClose is true, the database is forcibly closed. After the database is closed, open cannot be used to open the database.
When forceClose is false, the database is not forced to close. When the database is closed, it can be opened again using open.
When foreClose is true:
Copy code 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});//Create the server server where the database is located
var db=new mongo.Db("node-mongo-examples",server,{safe:true});//Create database object
db.open(function (err,db) {//Connect to database
If(err)
throw err;
else{
db.collection("users", function (err,collection) {
collection.insert({username:"Panpan", firstname:"李"}, 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:"三", firstname:"张"}, function (err,docs) {
If(err) throw err;
else{
console.log(docs);
db.close(true);
}
})
});
});
}
});
//Read data
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
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","三"]}}).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
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;
Since
console.log(docs);
}
}
);
}
})
}
});
});
The expression of or in query
:
Copy code