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.dhdzp.com/article/58815.htm
http://www.dhdzp.com/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)生的問題
- 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)文章
安裝nvm并使用nvm安裝nodejs及配置環(huán)境變量的全過程
有時(shí)候使用nvm管理node會(huì)發(fā)現(xiàn)無法使用node或npm,主要原因是環(huán)境變量沒有配置成功,下面這篇文章主要給大家介紹了關(guān)于安裝nvm并使用nvm安裝nodejs及配置環(huán)境變量的相關(guān)資料,需要的朋友可以參考下2023-03-03
基于nodejs使用express創(chuàng)建web服務(wù)器的操作步驟
express實(shí)際上是對(duì)nodejs內(nèi)置http進(jìn)行封裝后的第三方包,其中提供了快捷創(chuàng)建web服務(wù)器以及處理請(qǐng)求路由的方法,使我們可以更加方便快捷的實(shí)現(xiàn)一個(gè)web服務(wù)器項(xiàng)目,本文件給大家詳細(xì)介紹基于nodejs使用express?創(chuàng)建web服務(wù)器的操作步驟2023-07-07
詳解Node.js amqplib 連接 Rabbit MQ最佳實(shí)踐
這篇文章主要介紹了詳解Node.js amqplib 連接 Rabbit MQ最佳實(shí)踐,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
Nodejs實(shí)現(xiàn)定時(shí)爬蟲的完整實(shí)例
這篇文章主要給大家介紹了關(guān)于Nodejs實(shí)現(xiàn)定時(shí)爬蟲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Node解決簡(jiǎn)單重復(fù)問題系列之Excel內(nèi)容的獲取
這篇文章主要給大家介紹了關(guān)于利用Node解決簡(jiǎn)單重復(fù)問題系列之Excel內(nèi)容獲取的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧。2018-01-01
通過NodeJS輕松使用GRPC和協(xié)議緩沖區(qū)的方法
本文介紹了GRPC和協(xié)議緩沖區(qū)的基本概念,并展示了如何在NodeJS應(yīng)用程序中使用它們,GRPC是一個(gè)高性能RPC框架,協(xié)議緩沖區(qū)則用于定義服務(wù)和序列化消息,本文給大家介紹如何在NodeJS應(yīng)用程序中使用GRPC和協(xié)議緩沖區(qū),感興趣的朋友一起看看吧2024-10-10

