這個方法用來將Mongo DB伺服器與我們的Node應用程式連接起來。這是MongoDB模組中的一個非同步方法。
mongodb.connect(path[, callback])
#•path – 實際執行MongoDB伺服器的伺服器路徑和連接埠。
•callback – 如果發生任何錯誤,此函數將提供回呼。
npm install mongodb –save
mongod --dbpath=data --bind_ip 127.0.0.1
node MongodbConnect.jsExample
// 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中文網其他相關文章!