DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> node.js操作mongoDB數據庫示例分享
node.js操作mongoDB數據庫示例分享
編輯:關於JavaScript     

連接數據庫

代碼如下:
 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數據庫的講解,今天就先到這裡了,基本上常用的操作都有了示例,復雜些的,小伙伴們自由發揮吧,有機會我們再來繼續講解。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved