怎么在Node.js中操作MongoDB數(shù)據(jù)庫(kù)?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
成都創(chuàng)新互聯(lián)一直秉承“誠(chéng)信做人,踏實(shí)做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務(wù)為基礎(chǔ),以質(zhì)量求生存,以技術(shù)求發(fā)展,成交一個(gè)客戶多一個(gè)朋友!為您提供網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、成都網(wǎng)頁(yè)設(shè)計(jì)、成都小程序開(kāi)發(fā)、成都網(wǎng)站開(kāi)發(fā)、成都網(wǎng)站制作、成都軟件開(kāi)發(fā)、成都app軟件開(kāi)發(fā)公司是成都本地專業(yè)的網(wǎng)站建設(shè)和網(wǎng)站設(shè)計(jì)公司,等你一起來(lái)見(jiàn)證!
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(); }) });
看完上述內(nèi)容,你們掌握怎么在Node.js中操作MongoDB數(shù)據(jù)庫(kù)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!