溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Node.js中操作MongoDB數(shù)據(jù)庫

發(fā)布時(shí)間:2021-05-31 16:55:17 來源:億速云 閱讀:113 作者:Leah 欄目:web開發(fā)

怎么在Node.js中操作MongoDB數(shù)據(jù)庫?相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

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"
 }
}

連接數(shù)據(jù)庫

// 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();
 })
});

看完上述內(nèi)容,你們掌握怎么在Node.js中操作MongoDB數(shù)據(jù)庫的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI