Node. js operation mongoDB database sample sharing
- 2020-03-30 04:26:09
- OfStack
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});//Server
where the database is created
var db=new mongo.Db("node-mongo-examples",server,{safe:true});//Create the database object
db.open(function (err,db) {//Connect to 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(" Database closed successfully .");
});
Insert data :
After the data is inserted, the contents of the data document are output 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});//Server
where the database is created
var db=new mongo.Db("node-mongo-examples",server,{safe:true});//Create the database object
db.open(function (err,db) {//Connect to database
if(err)
throw err;
else{
db.collection("users", function (err,collection) {
collection.insert({username:" section ",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 the database The close ([forceClose], [callback]);
When forceClose is true, force the database to be shut down. When the database is shut down, you can no longer use open to start the database.
When forceClose is false, you do not force the database to be closed. When the database is closed, you can open it again with open.
When foreClose is true:
var mongo=require("mongodb");
var host="localhost";
var port=mongo.Connection.DEFAULT_PORT;
var server=new mongo.Server(host,port,{auto_reconnect:true});//Server
where the database is created
var db=new mongo.Db("node-mongo-examples",server,{safe:true});//Create the database object
db.open(function (err,db) {//Connect to database
if(err)
throw err;
else{
db.collection("users", function (err,collection) {
collection.insert({username:" section ",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 the 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 criteria
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:[" Delay, "," three "]}}).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;
else{
console.log(docs);
db.close();
}
}
);
}
})
}
});
});
The expression of or in a query :
collection.find({$or:[
{type:"food"},
{price:{$lt:10}}
]})
About the operation of node.js mongoDB database, today is the first here, basically common operations have examples, some complex, partners free play, we have the opportunity to continue to explain.