node.js操作mongoDB数据库示例分享

连接数据库

 var mongo=require("mongodb");

 var host="localhost";

 var port=mongo.Connection.DEFAULT_PORT;

 var server=new mongo.Server(host,port,{auto_reconnect:true});//创建数据库所在的服务器服务器

 var db=new mongo.Db("node-mongo-examples",server,{safe:true});//创建数据库对象

 db.open(function (err,db) {//连接数据库

     if(err)

         throw err;

     else{

         console.log("成功建立数据库连接");

         db.close();

     }

 });

 db.on("close", function (err,db) {//关闭数据库

     if(err) throw err;

     else console.log("成功关闭数据库.");

 });

插入数据:

插入数据后,在控制台中输出数据文档的内容

 var mongo=require("mongodb");

 var host="localhost";

 var port=mongo.Connection.DEFAULT_PORT;

 var server=new mongo.Server(host,port,{auto_reconnect:true});//创建数据库所在的服务器服务器

 var db=new mongo.Db("node-mongo-examples",server,{safe:true});//创建数据库对象

 db.open(function (err,db) {//连接数据库

     if(err)

         throw err;

     else{

         db.collection("users", function (err,collection) {

             collection.insert({username:"盼盼",firstname:"李"}, function (err,docs) {

                 console.log(docs);

                 db.close();

             });

         });       

     }

 });

 db.on("close", function (err,db) {//关闭数据库

     if(err) throw err;

     else console.log("成功关闭数据库.");

 });

关闭数据库db.close([forceClose],[callback]);

forceClose为true时,强制关闭该数据库,当数据库关闭后,不可再使用open开启数据库.

forceClose为false时,不强制关闭数据库,当数据库关闭后,可以再使用open打开.

当foreClose为true时:

 var mongo=require("mongodb");

 var host="localhost";

 var port=mongo.Connection.DEFAULT_PORT;

 var server=new mongo.Server(host,port,{auto_reconnect:true});//创建数据库所在的服务器服务器

 var db=new mongo.Db("node-mongo-examples",server,{safe:true});//创建数据库对象

 db.open(function (err,db) {//连接数据库

     if(err)

         throw err;

     else{

         db.collection("users", function (err,collection) {

             collection.insert({username:"盼盼",firstname:"李"}, function (err,docs) {

                 console.log(docs);

                 db.close(false);

             });

         });

     }

 });

 db.once("close", function (err,db) {//关闭数据库

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

                     }

                 })

             });

         });

     }

 });

//读取数据

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

                }

            });

        }

    });

});

//带查询条件的搜索

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:["延思","三"]}}).toArray(function(err,docs){

                if(err) throw  err;

                else{

                    console.log(docs);

                    db.close();

                }

            });

        }

    });

});

//插入一批数据,并且进行搜索type==food且price字段值小于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();

                            }

                        }

                    );

                }

            })

        }

    });

});

查询中的或的表达:

collection.find({$or:[

    {type:"food"},

    {price:{$lt:10}}

  ]})

有关node.js操作mongoDB数据库的讲解,今天就先到这里了,基本上常用的操作都有了示例,复杂些的,小伙伴们自由发挥吧,有机会我们再来继续讲解。

以上是 node.js操作mongoDB数据库示例分享 的全部内容, 来源链接: utcz.com/z/326028.html

回到顶部