当前位置:首页 > python教程 > python基础

Python MongoDB 插入文档

  • mongodb 创建集合

mongodb 中的文档与 sql 数据库中的记录相同。

插入集合

要在 mongodb 中把记录或我们所称的文档插入集合,我们使用 insert_one() 方法。

insert_one() 方法的第一个参数是字典,其中包含希望插入文档中的每个字段名称和值。

实例

在 "customers" 集合中插入记录:

import pymongo

myclient = pymongo.mongoclient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]

mydict = { "name": "bill", "address": "highway 37" }

x = mycol.insert_one(mydict)

运行实例

返回 _id 字段

insert_one() 方法返回 insertoneresult 对象,该对象拥有属性 inserted_id,用于保存插入文档的 id。

实例

在 "customers" 集合中插入另一条记录,并返回 _id 字段的值:

mydict = { "name": "peter", "address": "lowstreet 27" }

x = mycol.insert_one(mydict)

print(x.inserted_id)

运行实例

如果您没有指定 _id 字段,那么 mongodb 将为您添加一个,并为每个文档分配一个唯一的 id。

在上例中,没有指定 _id 字段,因此 mongodb 为记录(文档)分配了唯一的 _id。

插入多个文档

要将多个文档插入 mongodb 中的集合,我们使用 insert_many() 方法。

insert_many() 方法的第一个参数是包含字典的列表,其中包含要插入的数据:

实例
import pymongo

myclient = pymongo.mongoclient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]

mylist = [
  { "name": "amy", "address": "apple st 652"},
  { "name": "hannah", "address": "mountain 21"},
  { "name": "michael", "address": "valley 345"},
  { "name": "sandy", "address": "ocean blvd 2"},
  { "name": "betty", "address": "green grass 1"},
  { "name": "richard", "address": "sky st 331"},
  { "name": "susan", "address": "one way 98"},
  { "name": "vicky", "address": "yellow garden 2"},
  { "name": "ben", "address": "park lane 38"},
  { "name": "william", "address": "central st 954"},
  { "name": "chuck", "address": "main road 989"},
  { "name": "viola", "address": "sideway 1633"}
]

x = mycol.insert_many(mylist)

# 打印被插入文档的  _id 值列表:
print(x.inserted_ids)

运行实例

insert_many() 方法返回 insertmanyresult 对象,该对象拥有属性 inserted_ids,用于保存被插入文档的 id。

插入带有指定 id 的多个文档

如果您不希望 mongodb 为您的文档分配唯一 id,则可以在插入文档时指定 _id 字段。

请记住,值必须是唯一的。两个文件不能有相同的 _id。

实例
import pymongo

myclient = pymongo.mongoclient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]

mylist = [
  { "_id": 1, "name": "john", "address": "highway 37"},
  { "_id": 2, "name": "peter", "address": "lowstreet 27"},
  { "_id": 3, "name": "amy", "address": "apple st 652"},
  { "_id": 4, "name": "hannah", "address": "mountain 21"},
  { "_id": 5, "name": "michael", "address": "valley 345"},
  { "_id": 6, "name": "sandy", "address": "ocean blvd 2"},
  { "_id": 7, "name": "betty", "address": "green grass 1"},
  { "_id": 8, "name": "richard", "address": "sky st 331"},
  { "_id": 9, "name": "susan", "address": "one way 98"},
  { "_id": 10, "name": "vicky", "address": "yellow garden 2"},
  { "_id": 11, "name": "ben", "address": "park lane 38"},
  { "_id": 12, "name": "william", "address": "central st 954"},
  { "_id": 13, "name": "chuck", "address": "main road 989"},
  { "_id": 14, "name": "viola", "address": "sideway 1633"}
]

x = mycol.insert_many(mylist)

# 打印被插入文档的  _id 值列表:
print(x.inserted_ids)

运行实例

  • mongodb 创建集合

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!