
- 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 - 插入文件
您可以使用insert()方法將文件儲存到 MongoDB 中。此方法接受 JSON 文件作為引數。
語法
以下是 insert 方法的語法。
>db.COLLECTION_NAME.insert(DOCUMENT_NAME)
示例
> use mydb switched to db mydb > db.createCollection("sample") { "ok" : 1 } > doc1 = {"name": "Ram", "age": "26", "city": "Hyderabad"} { "name" : "Ram", "age" : "26", "city" : "Hyderabad" } > db.sample.insert(doc1) WriteResult({ "nInserted" : 1 }) >
同樣,您也可以使用insert()方法插入多個文件。
> use testDB switched to db testDB > db.createCollection("sample") { "ok" : 1 } > data = [ { "_id": "1001", "name": "Ram", "age": "26", "city": "Hyderabad" }, { "_id": "1002", "name" : "Rahim", "age" : 27, "city" : "Bangalore" }, { "_id": "1003", "name" : "Robert", "age" : 28, "city" : "Mumbai" } ] [ { "_id" : "1001", "name" : "Ram", "age" : "26", "city" : "Hyderabad" }, { "_id" : "1002", "name" : "Rahim", "age" : 27, "city" : "Bangalore" }, { "_id" : "1003", "name" : "Robert", "age" : 28, "city" : "Mumbai" } ] > db.sample.insert(data) BulkWriteResult ({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] }) >
使用 python 建立集合
Pymongo 提供了一個名為 insert_one() 的方法來在 MangoDB 中插入文件。對於此方法,我們需要以字典格式傳遞文件。
示例
以下示例將文件插入名為 example 的集合中。
from pymongo import MongoClient #Creating a pymongo client client = MongoClient('localhost', 27017) #Getting the database instance db = client['mydb'] #Creating a collection coll = db['example'] #Inserting document into a collection doc1 = {"name": "Ram", "age": "26", "city": "Hyderabad"} coll.insert_one(doc1) print(coll.find_one())
輸出
{ '_id': ObjectId('5d63ad6ce043e2a93885858b'), 'name': 'Ram', 'age': '26', 'city': 'Hyderabad' }
要使用 pymongo 將多個文件插入 MongoDB,您需要呼叫 insert_many() 方法。
from pymongo import MongoClient #Creating a pymongo client client = MongoClient('localhost', 27017) #Getting the database instance db = client['mydb'] #Creating a collection coll = db['example'] #Inserting document into a collection data = [ { "_id": "101", "name": "Ram", "age": "26", "city": "Hyderabad" }, { "_id": "102", "name": "Rahim", "age": "27", "city": "Bangalore" }, { "_id": "103", "name": "Robert", "age": "28", "city": "Mumbai" } ] res = coll.insert_many(data) print("Data inserted ......") print(res.inserted_ids)
輸出
Data inserted ...... ['101', '102', '103']
廣告