鍖椾含cba璧涚▼ :Node.js操作MongoDB數據庫實例分析

cba广东队赛程 www.axwwg.com  更新時間:2020-01-26 01:01:27   作者:佚名   我要評論(0)

本文實例講述了Node.js操作MongoDB數據庫。分享給大家供大家參考,具體如下:
Node.js操作MongoDB


npm init
npm i mongodb --save





{
"name": "test",
"ver

本文實例講述了Node.js操作MongoDB數據庫。分享給大家供大家參考,具體如下:

Node.js操作MongoDB

npm init
npm i mongodb --save

{
 "name": "test",
 "version": "1.0.0",
 "description": "",
 "main": "app.js",
 "scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
 },
 "author": "",
 "license": "ISC",
 "dependencies": {
  "mongodb": "^3.1.1"
 }
}

連接數據庫

// connect.js
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mydatabase';
// Use connect method to connect to the server
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
 console.log("Connected successfully to server");
 const db = client.db(dbName);
 client.close();
});

插入

// insert.js
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mydatabase';
// 插入
var insertData = function (db, callback) {
 // 獲取文檔集合
 var collection = db.collection('collection3');
 var data = [{"name": "李二狗001", "age": 20}, {"name": "李二狗002", "age": 21}];
 // 插入文檔
 collection.insert(data, function (err, result) {
  if(err) {
   console.log('Error: ' + err);
   return;
  }
  callback(result);
 })
}
// Use connect method to connect to the server
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
 console.log("Connected successfully to server");
 const db = client.db(dbName);
 insertData(db, function (result) {
  console.log(result);
  client.close();
 });
});

查詢

// find.js
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mydatabase';
// 查詢
var findData = function (db, callback) {
 // 獲取文檔集合
 var collection = db.collection('collection3');
 var whereStr = {"name": "李二狗001"};
 // 查詢文檔
 collection.find(whereStr).toArray(function (err, result) {
  if(err) {
   console.log('Error: ' + err);
   return;
  }
  callback(result);
 })
}
// Use connect method to connect to the server
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
 console.log("Connected successfully to server");
 const db = client.db(dbName);
 findData(db, function (result) {
  console.log(result);
  client.close();
 })
});

修改

// update.js
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mydatabase';
// 修改
var updateData = function (db, callback) {
 // 獲取文檔集合
 var collection = db.collection('collection3');
 var whereStr = {"name": "李二狗002"};
 var updateStr = {$set: {"age": 100}};
 // 修改文檔
 collection.update(whereStr, updateStr, function (err, result) {
  if(err) {
   console.log('Error: ' + err);
   return;
  }
  callback(result);
 })
}
// Use connect method to connect to the server
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
 console.log("Connected successfully to server");
 const db = client.db(dbName);
 updateData(db, function (result) {
  console.log(result);
  client.close();
 })
});

刪除

// delete.js
const MongoClient = require('mongodb').MongoClient;
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'mydatabase';
// 刪除
var delData = function (db, callback) {
 // 獲取文檔集合
 var collection = db.collection('collection3');
 var whereStr = {"name": "李二狗002"};
 // 刪除文檔
 collection.remove(whereStr, function (err, result) {
  if(err) {
   console.log('Error: ' + err);
   return;
  }
  callback(result);
 })
}
// Use connect method to connect to the server
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
 console.log("Connected successfully to server");
 const db = client.db(dbName);
  delData(db, function (result) {
  console.log(result);
  client.close();
 })
});

參考:

https://www.npmjs.com/package/mongodb
https://www.jb51.net/article/58815.htm
https://www.jb51.net/article/98813.htm

希望本文所述對大家node.js程序設計有所幫助。

您可能感興趣的文章:

  • node.js操作mongoDB數據庫示例分享
  • node.js連接mongoDB數據庫 快速搭建自己的web服務
  • Node.js對MongoDB數據庫實現模糊查詢的方法
  • 了不起的node.js讀書筆記之mongodb數據庫交互
  • node.js連接MongoDB數據庫的2種方法教程
  • Node.js連接MongoDB數據庫產生的問題
  • Node.js中使用mongoose操作mongodb數據庫的方法
  • 利用Vue.js+Node.js+MongoDB實現一個博客系統(附源碼)
  • 利用node.js+mongodb如何搭建一個簡單登錄注冊的功能詳解
  • 詳解Wondows下Node.js使用MongoDB的環境配置

相關文章

  • Node.js操作MongoDB數據庫實例分析

    Node.js操作MongoDB數據庫實例分析

    本文實例講述了Node.js操作MongoDB數據庫。分享給大家供大家參考,具體如下: Node.js操作MongoDB npm init npm i mongodb --save { "name": "test", "ver
    2020-01-26

最新評論