連接數(shù)據(jù)庫
代碼如下:
var mongo=require("mongodb");
var host="localhost";
var port=mongo.Connection.DEFAULT_PORT;
var server=new mongo.Server(host,port,{auto_reconnect:true});//創(chuàng)建數(shù)據(jù)庫所在的服務(wù)器服務(wù)器
var db=new mongo.Db("node-mongo-examples",server,{safe:true});//創(chuàng)建數(shù)據(jù)庫對象
db.open(function (err,db) {//連接數(shù)據(jù)庫
if(err)
throw err;
else{
console.log("成功建立數(shù)據(jù)庫連接");
db.close();
}
});
db.on("close", function (err,db) {//關(guān)閉數(shù)據(jù)庫
if(err) throw err;
else console.log("成功關(guān)閉數(shù)據(jù)庫.");
});
插入數(shù)據(jù):
插入數(shù)據(jù)后,在控制臺中輸出數(shù)據(jù)文檔的內(nèi)容
代碼如下:
var mongo=require("mongodb");
var host="localhost";
var port=mongo.Connection.DEFAULT_PORT;
var server=new mongo.Server(host,port,{auto_reconnect:true});//創(chuàng)建數(shù)據(jù)庫所在的服務(wù)器服務(wù)器
var db=new mongo.Db("node-mongo-examples",server,{safe:true});//創(chuàng)建數(shù)據(jù)庫對象
db.open(function (err,db) {//連接數(shù)據(jù)庫
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) {//關(guān)閉數(shù)據(jù)庫
if(err) throw err;
else console.log("成功關(guān)閉數(shù)據(jù)庫.");
});
關(guān)閉數(shù)據(jù)庫db.close([forceClose],[callback]);
forceClose為true時,強(qiáng)制關(guān)閉該數(shù)據(jù)庫,當(dāng)數(shù)據(jù)庫關(guān)閉后,不可再使用open開啟數(shù)據(jù)庫.
forceClose為false時,不強(qiáng)制關(guān)閉數(shù)據(jù)庫,當(dāng)數(shù)據(jù)庫關(guān)閉后,可以再使用open打開.
當(dāng)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});//創(chuàng)建數(shù)據(jù)庫所在的服務(wù)器服務(wù)器
var db=new mongo.Db("node-mongo-examples",server,{safe:true});//創(chuàng)建數(shù)據(jù)庫對象
db.open(function (err,db) {//連接數(shù)據(jù)庫
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) {//關(guān)閉數(shù)據(jù)庫
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);
}
})
});
});
}
});
//讀取數(shù)據(jù)
代碼如下:
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();
}
});
}
});
});
//插入一批數(shù)據(jù),并且進(jìn)行搜索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();
}
}
);
}
})
}
});
});
查詢中的或的表達(dá):
代碼如下:
collection.find({$or:[
{type:"food"},
{price:{$lt:10}}
]})
有關(guān)node.js操作mongoDB數(shù)據(jù)庫的講解,今天就先到這里了,基本上常用的操作都有了示例,復(fù)雜些的,小伙伴們自由發(fā)揮吧,有機(jī)會我們再來繼續(xù)講解。
更多信息請查看IT技術(shù)專欄