
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境設定
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 快速入門
- Node.js - MySQL 資料庫建立
- Node.js - MySQL 表建立
- Node.js - MySQL 資料插入
- Node.js - MySQL 資料查詢
- Node.js - MySQL 條件查詢 (Where)
- Node.js - MySQL 排序 (Order By)
- Node.js - MySQL 資料刪除
- Node.js - MySQL 資料更新
- Node.js - MySQL 連線查詢 (Join)
- Node.js MongoDB
- Node.js - MongoDB 快速入門
- Node.js - MongoDB 資料庫建立
- Node.js - MongoDB 集合建立
- Node.js - MongoDB 資料插入
- Node.js - MongoDB 資料查詢
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 資料刪除
- Node.js - MongoDB 資料更新
- Node.js - MongoDB 資料限制 (Limit)
- Node.js - MongoDB 連線查詢 (Join)
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
Node.js - MongoDB 資料庫建立
MongoDB 是一個開源的、跨平臺的面向文件的資料庫環境。MongoDB 使用類似 JSON 的、無模式的文件。多個文件構成一個集合,一個或多個集合可以存在於 MongoDB 資料庫中。在本章中,我們將學習如何使用 Node.js 應用程式建立 MongoDB 資料庫。
MongoDB 資料庫管理軟體提供以下三個版本:
MongoDB 社群版 - MongoDB 的原始碼可用、免費使用和自管理版本,可用於本地安裝,適用於 Windows、Linux 和 macOS。
MongoDB 企業版 - MongoDB 的商業版、訂閱版、自管理版本,具有比社群版更多的高階功能。
MongoDB Atlas - MongoDB 部署的按需、完全託管的雲服務。它執行在 AWS、Microsoft Azure 和 Google Cloud Platform 上。
連線字串
Node.js 的 mongodb 驅動程式使用 require() 函式匯入到程式碼中。MongoClient 類的物件表示資料庫連線。您需要將連線字串傳遞給其建構函式。
const { MongoClient } = require('mongodb'); const client = new MongoClient(ConnectionString);
MongoDB 連線字串必須採用以下格式之一:
標準連線字串格式 - 此格式用於連線到自託管的 MongoDB 單機部署、副本集或分片叢集。標準 URI 連線方案具有以下形式:
mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
預設連線字串為:
mongodb://:27017/
SRV 連線格式 - 連線字串的主機名對應於 DNS SRV 記錄。MongoDB Atlas 使用 SRV 連線格式。MongoDB 支援 DNS 構造的種子列表。它允許更大的部署靈活性以及在無需重新配置客戶端的情況下輪換伺服器的能力。SRV URI 連線方案具有以下形式:
mongodb+srv://[username:password@]host[/[defaultauthdb][?options]]
這是一個 SRV 連線字串的示例:
const uri = "mongodb+srv://user:pwd@cluster0.zhmrg1h.mongodb.net/?retryWrites=true&w=majority";
在下面的示例中,我們使用標準連線字串來獲取資料庫列表。
示例:資料庫列表
MongoClient 物件的 connect() 方法返回一個 Promise。對 connect() 方法的呼叫是非同步的。接下來呼叫 listdatabases() 函式。
const { MongoClient } = require('mongodb'); async function main() { const uri = "mongodb://:27017/"; const client = new MongoClient(uri); try { // Connect to the MongoDB cluster await client.connect(); // Make the appropriate DB calls await listDatabases(client); } catch (e) { console.error(e); } finally { // Close the connection to the MongoDB cluster await client.close(); } } main().catch(console.error); async function listDatabases(client) { databasesList = await client.db().admin().listDatabases(); console.log("Databases:"); databasesList.databases.forEach(db => console.log(` - ${db.name}`)); };
輸出
Databases: - admin - config - local - myProject - mydb
建立新的資料庫
要在伺服器上建立新的資料庫,您需要呼叫 MongoClient 類的 db() 方法。
var dbobj = client.db(database_name, options);
db() 方法返回一個 db 例項,該例項代表伺服器上的資料庫。在下面的示例中,我們演示瞭如何在 MongoDB 中建立一個名為 mydatabase 的新資料庫。
const {MongoClient} = require('mongodb'); async function main(){ const uri = "mongodb://:27017/mydb"; const client = new MongoClient(uri); try { // Connect to the MongoDB cluster await client.connect(); await createdb(client, "mydatabase"); } finally { // Close the connection to the MongoDB cluster await client.close(); } } main().catch(console.error); async function createdb(client, dbname){ const dbobj = await client.db(dbname); console.log("Database created"); console.log(dbobj); }
但是,請注意,資料庫不會在伺服器上物理建立,除非至少建立了一個集合,並且其中至少包含一個文件。