本篇文章給大家分享的是有關(guān)使用Nodejs怎么連接mongodb數(shù)據(jù)庫,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

1. 創(chuàng)建package.json
首先,創(chuàng)建我們的工程目錄connect-mongodb,并作為我們的當(dāng)前目錄
mkdir connect-mongodb cd connect-mongodb
輸入npm init命令創(chuàng)建package.json
npm init
然后,安裝mongodb的nodejs版本driver
npm install mongodb --save
mongodb驅(qū)動(dòng)包將會(huì)安裝到當(dāng)前目錄下的node_modules中
2. 啟動(dòng)MongoDB服務(wù)器
安裝MongoDB并啟動(dòng)MongoDB數(shù)據(jù)庫服務(wù),可參考我之前的文章,或者M(jìn)ongoDB官方文檔
3. 連接MongoDB
創(chuàng)建一個(gè)app.js文件,并添加以下代碼來連接服務(wù)器地址為192.168.0.243,mongodb端口為27017上名稱為myNewDatabase的數(shù)據(jù)庫
var MongoClient = require('mongodb').MongoClient,
assert = require('assert');
// Connection URL
var url = 'mongodb://192.168.0.243:27017/myNewDatabase';
MongoClient.connect(url,function(err,db){
assert.equal(null,err);
console.log("Connection successfully to server");
db.close();
});在命令行輸入以下命令運(yùn)行app.js
node app.js
4. 插入文檔
在app.js中添加以下代碼,使用insertMany方法添加3個(gè)文檔到documents集合中
var insertDocuments = function(db, callback){
// get ths documents collection
var collection = db.collection('documents');
// insert some documents
collection.insertMany([
{a:1},{a:2},{a:3}
],function(err,result){
assert.equal(err,null);
assert.equal(3,result.result.n);
assert.equal(3,result.ops.length);
console.log("Inserted 3 documents into the collection");
callback(result);
});
};insert命令返回一個(gè)包含以下屬性的對(duì)象:
result MongoDB返回的文檔結(jié)果
ops 添加了_id字段的文檔
connection 執(zhí)行插入操作所使用的connection
在app.js更新以下代碼調(diào)用insertDocuments方法
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to server");
insertDocuments(db, function() {
db.close();
});
});在命令行中使用node app.js運(yùn)行
5. 查詢所有文檔
添加findDocuments函數(shù)
var findDocuments = function(db,callback){
// get the documents collection
var collection = db.collection('documents');
// find some documents
collection.find({}).toArray(function(err,docs){
assert.equal(err,null);
console.log("Found the following records");
console.log(docs);
callback(docs);
});
};findDocuments函數(shù)查詢了所有'documents'集合中所有的文檔,將此函數(shù)添加到MongoClient.connect的回調(diào)函數(shù)中
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
insertDocuments(db, function() {
findDocuments(db, function() {
db.close();
});
});
});6. 使用過濾條件(query filter)查詢文檔
查詢'a':3的文檔
var findDocuments = function(db, callback) {
// Get the documents collection
var collection = db.collection('documents');
// Find some documents
collection.find({'a': 3}).toArray(function(err, docs) {
assert.equal(err, null);
console.log("Found the following records");
console.log(docs);
callback(docs);
});
}7. 更新文檔
var updateDocument = function(db,callback){
// get the documents collection
var collection = db.collection('documents');
// update document where a is 2, set b equal to 1
collection.updateOne({a:2},{
$set:{b:1}
},function(err,result){
assert.equal(err,null);
assert.equal(1,result.result.n);
console.log("updated the document with the field a equal to 2");
callback(result);
});
};updateDocument方法更新滿足條件a為2的第一個(gè)文檔,新增一個(gè)b屬性,并將其設(shè)置為1。
將updateDocument方法添加到MongoClient.connect方法的回調(diào)中
MongoClient.connect(url,function(err,db){
assert.equal(null,err);
console.log("Connection successfully to server");
insertDocuments(db,function(){
updateDocument(db,function(){
db.close();
});
});
});8. 刪除文檔
var removeDocument = function(db,callback){
// get the documents collection
var collection = db.collection('documents');
// remove some documents
collection.deleteOne({a:3},function(err,result){
assert.equal(err,null);
assert.equal(1,result.result.n);
console.log("removed the document with the field a equal to 3");
callback(result);
});
};添加到app.js中
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to server");
insertDocuments(db, function() {
updateDocument(db, function() {
removeDocument(db, function() {
db.close();
});
});
});
});9. 創(chuàng)建索引
索引能夠改善應(yīng)用的性能。下面你代碼在'a'屬性上添加索引
var indexCollection = function(db,callback){
db.collection('documents').createIndex({
a:1
},null,function(err,results){
console.log(results);
callback();
});
};更新app.js
MongoClient.connect(url,function(err,db){
assert.equal(null,err);
console.log("Connection successfully to server");
insertDocuments(db,function(){
indexCollection(db,function(){
db.close();
});
});
});以上就是使用Nodejs怎么連接mongodb數(shù)據(jù)庫,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)成都做網(wǎng)站行業(yè)資訊頻道。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)站題目:使用Nodejs怎么連接mongodb數(shù)據(jù)庫-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://chinadenli.net/article36/ccgesg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站、網(wǎng)站排名、全網(wǎng)營銷推廣、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容