Home > Web Front-end > JS Tutorial > body text

node.js operation mongoDB database example sharing_node.js

WBOY
Release: 2016-05-16 16:30:05
Original
1893 people have browsed it

Connect to database

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{
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

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();
             });
         });                                                    }
});
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

Copy code 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

Copy code 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","三"]}}).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

Copy code 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;
                                                                                                                                                                                                                                                                                                 Since console.log(docs);
                                                                                                                                                                                                                                                                                   }                                                                                                           } );
                }
             })
}
});
});




The expression of or in query
:

Copy code

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!