
- Python 資料訪問教程
- Python 資料訪問 - 首頁
- Python MySQL
- Python MySQL - 簡介
- Python MySQL - 資料庫連線
- Python MySQL - 建立資料庫
- Python MySQL - 建立表
- Python MySQL - 插入資料
- Python MySQL - 選擇資料
- Python MySQL - Where 子句
- Python MySQL - Order By
- Python MySQL - 更新表
- Python MySQL - 刪除資料
- Python MySQL - 刪除表
- Python MySQL - Limit
- Python MySQL - Join
- Python MySQL - 遊標物件
- Python PostgreSQL
- Python PostgreSQL - 簡介
- Python PostgreSQL - 資料庫連線
- Python PostgreSQL - 建立資料庫
- Python PostgreSQL - 建立表
- Python PostgreSQL - 插入資料
- Python PostgreSQL - 選擇資料
- Python PostgreSQL - Where 子句
- Python PostgreSQL - Order By
- Python PostgreSQL - 更新表
- Python PostgreSQL - 刪除資料
- Python PostgreSQL - 刪除表
- Python PostgreSQL - Limit
- Python PostgreSQL - Join
- Python PostgreSQL - 遊標物件
- Python SQLite
- Python SQLite - 簡介
- Python SQLite - 建立連線
- Python SQLite - 建立表
- Python SQLite - 插入資料
- Python SQLite - 選擇資料
- Python SQLite - Where 子句
- Python SQLite - Order By
- Python SQLite - 更新表
- Python SQLite - 刪除資料
- Python SQLite - 刪除表
- Python SQLite - Limit
- Python SQLite - Join
- Python SQLite - 遊標物件
- Python MongoDB
- Python MongoDB - 簡介
- Python MongoDB - 建立資料庫
- Python MongoDB - 建立集合
- Python MongoDB - 插入文件
- Python MongoDB - 查詢
- Python MongoDB - 查詢
- Python MongoDB - 排序
- Python MongoDB - 刪除文件
- Python MongoDB - 刪除集合
- Python MongoDB - 更新
- Python MongoDB - Limit
- Python 資料訪問資源
- Python 資料訪問 - 快速指南
- Python 資料訪問 - 有用資源
- Python 資料訪問 - 討論
Python MongoDB - 建立資料庫
與其他資料庫不同,MongoDB 沒有提供單獨的命令來建立資料庫。
通常,use 命令用於選擇/切換到特定的資料庫。此命令首先驗證我們指定的資料庫是否存在,如果存在,則連線到它。如果我們使用 use 命令指定的資料庫不存在,則會建立一個新的資料庫。
因此,您可以使用use 命令在 MongoDB 中建立資料庫。
語法
use DATABASE 語句的基本語法如下所示:
use DATABASE_NAME
示例
以下命令建立一個名為 mydb 的資料庫。
>use mydb switched to db mydb
您可以使用 db 命令驗證您的建立,這將顯示當前資料庫。
>db mydb
使用 python 建立資料庫
要使用 pymongo 連線到 MongoDB,您需要匯入並建立一個 MongoClient,然後您可以直接訪問您需要在 attribute passion 中建立的資料庫。
示例
以下示例在 MangoDB 中建立了一個數據庫。
from pymongo import MongoClient #Creating a pymongo client client = MongoClient('localhost', 27017) #Getting the database instance db = client['mydb'] print("Database created........") #Verification print("List of databases after creating new one") print(client.list_database_names())
輸出
Database created........ List of databases after creating new one: ['admin', 'config', 'local', 'mydb']
您還可以在建立 MongoClient 時指定埠和主機名,並可以以字典樣式訪問資料庫。
示例
from pymongo import MongoClient #Creating a pymongo client client = MongoClient('localhost', 27017) #Getting the database instance db = client['mydb'] print("Database created........")
輸出
Database created........
廣告