Node.js操作MongoDB數(shù)據(jù)庫(kù)實(shí)例分析
本文實(shí)例講述了Node.js操作MongoDB數(shù)據(jù)庫(kù)。分享給大家供大家參考,具體如下:
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ù)庫(kù)
// 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
http://www.dbjr.com.cn/article/58815.htm
http://www.dbjr.com.cn/article/98813.htm
希望本文所述對(duì)大家node.js程序設(shè)計(jì)有所幫助。
- Node.js中使用mongoose操作mongodb數(shù)據(jù)庫(kù)的方法
- node.js連接MongoDB數(shù)據(jù)庫(kù)的2種方法教程
- Node.js對(duì)MongoDB數(shù)據(jù)庫(kù)實(shí)現(xiàn)模糊查詢的方法
- Node.js連接MongoDB數(shù)據(jù)庫(kù)產(chǎn)生的問(wèn)題
- node.js連接mongoDB數(shù)據(jù)庫(kù) 快速搭建自己的web服務(wù)
- 了不起的node.js讀書筆記之mongodb數(shù)據(jù)庫(kù)交互
- node.js操作mongoDB數(shù)據(jù)庫(kù)示例分享
- Node.js中Mongodb數(shù)據(jù)庫(kù)操作方法(最新推薦)
相關(guān)文章
使用Node.js實(shí)現(xiàn)Clean?Architecture方法示例詳解
這篇文章主要為大家介紹了使用Node.js實(shí)現(xiàn)Clean?Architecture方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
npm安裝報(bào)錯(cuò)npm ERR! Error: EPERM: operation&
這篇文章主要為大家介紹了npm安裝報(bào)錯(cuò)npm ERR! Error: EPERM: operation not permitted解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
解決Mac安裝thrift因bison報(bào)錯(cuò)的問(wèn)題
今天小編就為大家分享一篇解決Mac安裝thrift因bison報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Node.js實(shí)現(xiàn)讀取Excel數(shù)據(jù)并插入MySQL
這篇文章主要為大家詳細(xì)介紹了Node.js如何實(shí)現(xiàn)讀取Excel數(shù)據(jù)并插入到MySQL數(shù)據(jù)庫(kù)中,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11
詳解NodeJS Https HSM雙向認(rèn)證實(shí)現(xiàn)
這篇文章主要介紹了詳解NodeJS Https HSM雙向認(rèn)證實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
Node.js成為Web應(yīng)用開(kāi)發(fā)最佳選擇的原因
本篇文章給大家詳細(xì)分析了Node.js 成為 Web 應(yīng)用開(kāi)發(fā)最佳選擇的十個(gè)原因,對(duì)此有需要的朋友參考下。2018-02-02
淺談node node-sass sass-loader版本對(duì)應(yīng)問(wèn)題
本文主要介紹了淺談node node-sass sass-loader版本對(duì)應(yīng)問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
node-red教程之dashboard簡(jiǎn)介與輸入型儀表板控件的使用
Node-red支持自定義節(jié)點(diǎn),當(dāng)然也就支持自定義圖形化的節(jié)點(diǎn)。也有優(yōu)秀的開(kāi)發(fā)者把自己建立的圖形化節(jié)點(diǎn)無(wú)償分享。這里給出一個(gè)股票界面的例子,讓大家看一看優(yōu)秀的node-red界面能做到什么樣子2022-01-01

