Python MongoDB 插入文档

MongoDB 中的文档与 SQL 数据库中的记录相同。


插入集合

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

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

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

  1. import pymongo
  2. myclient = pymongo.MongoClient('mongodb://localhost:27017/')
  3. mydb = myclient['mydatabase']
  4. mycol = mydb["customers"]
  5. mydict = { "name": "John", "address": "Highway 37" }
  6. x = mycol.insert_one(mydict)
  7. print(x)

返回 _id 字段

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

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

  1. import pymongo
  2. myclient = pymongo.MongoClient('mongodb://localhost:27017/')
  3. mydb = myclient['mydatabase']
  4. mycol = mydb["customers"]
  5. mydict = { "name": "Peter", "address": "Lowstreet 27" }
  6. x = mycol.insert_one(mydict)
  7. print(x.inserted_id)

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

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


插入多个文档

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

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

  1. import pymongo
  2. myclient = pymongo.MongoClient("mongodb://localhost:27017/")
  3. mydb = myclient["mydatabase"]
  4. mycol = mydb["customers"]
  5. mylist = [
  6. { "name": "Amy", "address": "Apple st 652"},
  7. { "name": "Hannah", "address": "Mountain 21"},
  8. { "name": "Michael", "address": "Valley 345"},
  9. { "name": "Sandy", "address": "Ocean blvd 2"},
  10. { "name": "Betty", "address": "Green Grass 1"},
  11. { "name": "Richard", "address": "Sky st 331"},
  12. { "name": "Susan", "address": "One way 98"},
  13. { "name": "Vicky", "address": "Yellow Garden 2"},
  14. { "name": "Ben", "address": "Park Lane 38"},
  15. { "name": "William", "address": "Central st 954"},
  16. { "name": "Chuck", "address": "Main Road 989"},
  17. { "name": "Viola", "address": "Sideway 1633"}
  18. ]
  19. x = mycol.insert_many(mylist)
  20. #打印插入文档的_id值列表:
  21. print(x.inserted_ids)

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


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

如果您不希望 MongoDB 为您的文档分配唯一 id,则可以在插入文档时指定 _id 字段。请记住,值必须是唯一的。两个文件不能有相同的 _id。

  1. import pymongo
  2. myclient = pymongo.MongoClient("mongodb://localhost:27017/")
  3. mydb = myclient["mydatabase"]
  4. mycol = mydb["customers"]
  5. mylist = [
  6. { "_id": 1, "name": "John", "address": "Highway 37"},
  7. { "_id": 2, "name": "Peter", "address": "Lowstreet 27"},
  8. { "_id": 3, "name": "Amy", "address": "Apple st 652"},
  9. { "_id": 4, "name": "Hannah", "address": "Mountain 21"},
  10. { "_id": 5, "name": "Michael", "address": "Valley 345"},
  11. { "_id": 6, "name": "Sandy", "address": "Ocean blvd 2"},
  12. { "_id": 7, "name": "Betty", "address": "Green Grass 1"},
  13. { "_id": 8, "name": "Richard", "address": "Sky st 331"},
  14. { "_id": 9, "name": "Susan", "address": "One way 98"},
  15. { "_id": 10, "name": "Vicky", "address": "Yellow Garden 2"},
  16. { "_id": 11, "name": "Ben", "address": "Park Lane 38"},
  17. { "_id": 12, "name": "William", "address": "Central st 954"},
  18. { "_id": 13, "name": "Chuck", "address": "Main Road 989"},
  19. { "_id": 14, "name": "Viola", "address": "Sideway 1633"}
  20. ]
  21. x = mycol.insert_many(mylist)
  22. #打印插入文档的_id值列表:
  23. print(x.inserted_ids)

分类导航