將MongoDB與NodeJS連接
mongodb.connect介紹
這個方法用來將Mongo DB伺服器與我們的Node應用程式連接起來。這是MongoDB模組中的一個非同步方法。
語法
mongodb.connect(path[, callback])
參數
#•path – 實際執行MongoDB伺服器的伺服器路徑和連接埠。
•callback – 如果發生任何錯誤,此函數將提供回呼。
安裝Mongo-DB
##在嘗試將應用程式與Nodejs連接之前,我們首先需要設定MongoDB伺服器。- 使用下列查詢從npm安裝mongoDB。
npm install mongodb –save
- 執行以下命令在特定的本機伺服器上設定您的mongoDB。這將有助於與MongoDB建立連線。
mongod --dbpath=data --bind_ip 127.0.0.1
- 建立一個MongodbConnect.js文件,並將以下程式碼片段複製並貼上到該文件中。
- 現在,執行以下命令來執行程式碼片段。
node MongodbConnect.js
// Calling the required MongoDB module.
const MongoClient = require("mongodb");
// Server path
const url = 'mongodb://localhost:27017/';
// Name of the database
const dbname = "Employee";
MongoClient.connect(url, (err,client)=>{
if(!err) {
console.log("successful connection with the server");
}
else
console.log("Error in the connectivity");
})
登入後複製輸出C:\Users\tutorialsPoint\> node MongodbConnect.js
(node:7016) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(Use `node --trace-deprecation ...` to show where the warning was created)
successful connection with the server.
登入後複製#####
// Calling the required MongoDB module. const MongoClient = require("mongodb"); // Server path const url = 'mongodb://localhost:27017/'; // Name of the database const dbname = "Employee"; MongoClient.connect(url, (err,client)=>{ if(!err) { console.log("successful connection with the server"); } else console.log("Error in the connectivity"); })
C:\Users\tutorialsPoint\> node MongodbConnect.js (node:7016) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. (Use `node --trace-deprecation ...` to show where the warning was created) successful connection with the server.
以上是將MongoDB與NodeJS連接的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

本文討論了各種MongoDB索引類型(單,化合物,多鍵,文本,地理空間)及其對查詢性能的影響。它還涵蓋了根據數據結構和查詢需求選擇正確索引的注意事項。

本文討論了在MongoDB中創建用戶和角色,管理權限,確保安全和自動化這些過程。它強調了最佳實踐,例如最低特權和基於角色的訪問控制。

本文討論了在MongoDB中選擇一個碎片鑰匙,並強調了其對性能和可伸縮性的影響。主要考慮因素包括高基數,查詢模式和避免單調增長。

MongoDB Compass是用於管理和查詢MongoDB數據庫的GUI工具。它提供數據探索,複雜查詢執行和數據可視化的功能。

本文討論了配置MongoDB審計安全性合規性,詳細介紹了啟用審核,設置審核過濾器並確保日誌符合監管標準的步驟。主要問題:適當的配置和分析審核日誌的安全

該文章指導了通過身份驗證和授權來實施和確保MongoDB,討論最佳實踐,基於角色的訪問控制以及對常見問題進行故障排除。

本文介紹瞭如何在MongoDB中使用MAP-REDUCE進行批處數據處理,其對大型數據集的績效益處,優化策略,並闡明了其對批處理而不是實時操作的適用性。

本文討論了一個碎片的MongoDB群集的組件:Mongos,Config Server和Shards。它著重於這些組件如何啟用有效的數據管理和可擴展性。
