欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Nodejs連接mongodb數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼

 更新時(shí)間:2017年08月21日 10:35:21   作者:秦ge  
這篇文章主要介紹了使用Nodejs連接mongodb數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼,需要的朋友可以參考下

一個(gè)簡(jiǎn)單的nodejs連接mongodb示例,來(lái)自 mongodb官方示例

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ù)庫(kù)服務(wù),可參考我之前的文章,或者MongoDB官方文檔

3. 連接MongoDB

創(chuàng)建一個(gè)app.js文件,并添加以下代碼來(lái)連接服務(wù)器地址為192.168.0.243,mongodb端口為27017上名稱為myNewDatabase的數(shù)據(jù)庫(kù)

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. 使用過(guò)濾條件(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();
    });
  });
});

代碼已經(jīng)托管在碼云

總結(jié)

以上所述是小編給大家介紹的使用Nodejs連接mongodb數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • node.js中的fs.linkSync方法使用說(shuō)明

    node.js中的fs.linkSync方法使用說(shuō)明

    這篇文章主要介紹了node.js中的fs.linkSync方法使用說(shuō)明,本文介紹了fs.linkSync的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下
    2014-12-12
  • Node.js 的異步 IO 性能探討

    Node.js 的異步 IO 性能探討

    Node.js 的賣點(diǎn)是「異步單線程」,雖然主流 Web 后端編程語(yǔ)言中,對(duì)異步編程有很好支持的語(yǔ)言并不少,但只有 Node.js 喪心病狂地將所有 IO 強(qiáng)制異步進(jìn)行。
    2014-10-10
  • 使用webpack打包koa2 框架app

    使用webpack打包koa2 框架app

    本文給大家介紹的是使用webpack為koa2框架打包的步驟及最終的部署,非常實(shí)用,有需要的小伙伴可以參考下
    2018-02-02
  • 詳解兩個(gè)Node.js進(jìn)程是如何通信

    詳解兩個(gè)Node.js進(jìn)程是如何通信

    進(jìn)程間通信是是Node.js的一個(gè)十分重要的部分,這篇文章主要給大家介紹了關(guān)于兩個(gè)Node.js進(jìn)程是如何通信的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-10-10
  • npm淘寶鏡像過(guò)期解決辦法

    npm淘寶鏡像過(guò)期解決辦法

    npm?官方鏡像在國(guó)內(nèi)訪問(wèn)很慢,很多同學(xué)都會(huì)選擇切換到國(guó)內(nèi)的一些 npm 鏡像,本文主要介紹了npm淘寶鏡像過(guò)期解決辦法,感興趣的可以了解一下
    2024-02-02
  • node.js基礎(chǔ)知識(shí)小結(jié)

    node.js基礎(chǔ)知識(shí)小結(jié)

    本文給大家匯總介紹了學(xué)習(xí)node.js的一些關(guān)于開(kāi)發(fā)環(huán)境的基礎(chǔ)知識(shí),非常簡(jiǎn)單,給新手們參考下
    2018-02-02
  • node thread.sleep實(shí)現(xiàn)示例

    node thread.sleep實(shí)現(xiàn)示例

    這篇文章主要介紹了node thread.sleep實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • NodeJS去除BOM和轉(zhuǎn)換UTF8編碼

    NodeJS去除BOM和轉(zhuǎn)換UTF8編碼

    使用NodeJS編寫前端工具時(shí),操作得最多的是文本文件,但遺憾的是,GBK編碼不在NodeJS自身支持范圍內(nèi),UTF8文件還可能帶有BOM,在讀取不同編碼的文本文件時(shí),需要將文件內(nèi)容轉(zhuǎn)換為JS使用的UTF8編碼字符串后才能正常處理
    2023-11-11
  • node環(huán)境執(zhí)行js文件的完整步驟

    node環(huán)境執(zhí)行js文件的完整步驟

    Nodejs下運(yùn)行JS代碼有兩種方式,一種是在Node.js的交互環(huán)境下運(yùn)行,另外一種是把代碼寫入文件中,然后用node命令執(zhí)行文件代碼,下面這篇文章主要給大家介紹了關(guān)于node環(huán)境執(zhí)行js文件的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • nodejs?快速入門之事件循環(huán)

    nodejs?快速入門之事件循環(huán)

    這篇文章主要介紹了nodejs?快速入門之事件循環(huán)的相關(guān)資料,需要的朋友可以參考下
    2023-05-05

最新評(píng)論