Python 数据访问教程

Python 数据访问 - 主页

Python MySQL

Python MySQL - 简介 Python MySQL - 数据库连接 Python MySQL - 创建数据库 Python MySQL - 创建表 Python MySQL - 插入数据 Python MySQL - 选择数据 Python MySQL - Where 子句 Python MySQL - 排序 Python MySQL - 更新表 Python MySQL - 删除数据 Python MySQL - 删除表 Python MySQL - Limit 子句 Python MySQL - 连接 Python MySQL - 游标对象

Python PostgreSQL

Python PostgreSQL - 简介 Python PostgreSQL - 数据库连接 Python PostgreSQL - 创建数据库 Python PostgreSQL - 创建表 Python PostgreSQL - 插入数据 Python PostgreSQL - 选择数据 Python PostgreSQL - Where 子句 Python PostgreSQL - 排序 Python PostgreSQL - 更新表 Python PostgreSQL - 删除数据 Python PostgreSQL - 删除表 Python PostgreSQL - Limit 子句 Python PostgreSQL - 连接 Python PostgreSQL - 游标对象

Python SQLite

Python SQLite - 简介 Python SQLite - 建立连接 Python SQLite - 创建表 Python SQLite - 插入数据 Python SQLite - 选择数据 Python SQLite - Where 子句 Python SQLite - 排序 Python SQLite - 更新表 Python SQLite - 删除数据 Python SQLite - 删除表 Python SQLite - Limit 子句 Python SQLite - 连接 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

#创建 pymongo 客户端
client = MongoClient('localhost', 27017)

#获取数据库实例
db = client['mydb']

#创建集合
coll = db['example']

#将文档插入集合
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

#创建 pymongo 客户端
client = MongoClient('localhost', 27017)

#获取数据库实例
db = client['mydb']

#创建集合
coll = db['example']

#将文档插入集合
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']