怎么在Node.js中操作MongoDB數(shù)據(jù)庫?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
成都創(chuàng)新互聯(lián)一直秉承“誠信做人,踏實做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務為基礎,以質量求生存,以技術求發(fā)展,成交一個客戶多一個朋友!為您提供網(wǎng)站設計、成都網(wǎng)站建設、成都網(wǎng)頁設計、成都小程序開發(fā)、成都網(wǎng)站開發(fā)、成都網(wǎng)站制作、成都軟件開發(fā)、成都app軟件開發(fā)公司是成都本地專業(yè)的網(wǎng)站建設和網(wǎng)站設計公司,等你一起來見證!
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ù)庫的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
本文名稱:怎么在Node.js中操作MongoDB數(shù)據(jù)庫
轉載來源:http://jinyejixie.com/article6/jjgsog.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設計、網(wǎng)站改版、商城網(wǎng)站、移動網(wǎng)站建設、網(wǎng)站內(nèi)鏈、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)