Python Mongodb 插入文档
最后更新于:2022-03-27 02:24:50
Python Mongodb 插入文档
MongoDB 中的一个文档类似 SQL 表中的一条记录。
插入集合
集合中插入文档使用 insert_one() 方法,该方法的第一参数是字典 name => value 对。
以下实例向 sites 集合中插入文档:
实例
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["runoobdb"]
mycol = mydb["sites"] mydict = { "name": "RUNOOB", "alexa": "10000", "url": "https://docs.gechiui.com/w3school" } x = mycol.insert_one(mydict)
print(x)
print(x)
mydb = myclient["runoobdb"]
mycol = mydb["sites"] mydict = { "name": "RUNOOB", "alexa": "10000", "url": "https://docs.gechiui.com/w3school" } x = mycol.insert_one(mydict)
print(x)
print(x)
执行输出结果为:
<pymongo.results.InsertOneResult object at 0x10a34b288>
返回 _id 字段
insert_one() 方法返回 InsertOneResult 对象,该对象包含 inserted_id 属性,它是插入文档的 id 值。
实例
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient(‘mongodb://localhost:27017/‘)
mydb = myclient[‘runoobdb‘]
mycol = mydb["sites"] mydict = { "name": "Google", "alexa": "1", "url": "https://www.google.com" } x = mycol.insert_one(mydict) print(x.inserted_id)
mydb = myclient[‘runoobdb‘]
mycol = mydb["sites"] mydict = { "name": "Google", "alexa": "1", "url": "https://www.google.com" } x = mycol.insert_one(mydict) print(x.inserted_id)
执行输出结果为:
5b2369cac315325f3698a1cf
如果我们在插入文档时没有指定 _id,MongoDB 会为每个文档添加一个唯一的 id。
插入多个文档
集合中插入多个文档使用 insert_many() 方法,该方法的第一参数是字典列表。
实例
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["runoobdb"]
mycol = mydb["sites"] mylist = [
{ "name": "Taobao", "alexa": "100", "url": "https://www.taobao.com" },
{ "name": "QQ", "alexa": "101", "url": "https://www.qq.com" },
{ "name": "Facebook", "alexa": "10", "url": "https://www.facebook.com" },
{ "name": "知乎", "alexa": "103", "url": "https://www.zhihu.com" },
{ "name": "Github", "alexa": "109", "url": "https://www.github.com" }
] x = mycol.insert_many(mylist) # 输出插入的所有文档对应的 _id 值
print(x.inserted_ids)
mydb = myclient["runoobdb"]
mycol = mydb["sites"] mylist = [
{ "name": "Taobao", "alexa": "100", "url": "https://www.taobao.com" },
{ "name": "QQ", "alexa": "101", "url": "https://www.qq.com" },
{ "name": "Facebook", "alexa": "10", "url": "https://www.facebook.com" },
{ "name": "知乎", "alexa": "103", "url": "https://www.zhihu.com" },
{ "name": "Github", "alexa": "109", "url": "https://www.github.com" }
] x = mycol.insert_many(mylist) # 输出插入的所有文档对应的 _id 值
print(x.inserted_ids)
输出结果类似如下:
[ObjectId('5b236aa9c315325f5236bbb6'), ObjectId('5b236aa9c315325f5236bbb7'), ObjectId('5b236aa9c315325f5236bbb8'), ObjectId('5b236aa9c315325f5236bbb9'), ObjectId('5b236aa9c315325f5236bbba')]
insert_many() 方法返回 InsertManyResult 对象,该对象包含 inserted_ids 属性,该属性保存着所有插入文档的 id 值。
执行完以上查找,我们可以在命令终端,查看数据是否已插入:
插入指定 _id 的多个文档
我们也可以自己指定 id,插入,以下实例我们在 site2 集合中插入数据,_id 为我们指定的:
实例
#!/usr/bin/python3
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["runoobdb"]
mycol = mydb["site2"] mylist = [
{ "_id": 1, "name": "RUNOOB", "cn_name": "菜鸟教程"},
{ "_id": 2, "name": "Google", "address": "Google 搜索"},
{ "_id": 3, "name": "Facebook", "address": "脸书"},
{ "_id": 4, "name": "Taobao", "address": "淘宝"},
{ "_id": 5, "name": "Zhihu", "address": "知乎"}
] x = mycol.insert_many(mylist) # 输出插入的所有文档对应的 _id 值
print(x.inserted_ids)
mydb = myclient["runoobdb"]
mycol = mydb["site2"] mylist = [
{ "_id": 1, "name": "RUNOOB", "cn_name": "菜鸟教程"},
{ "_id": 2, "name": "Google", "address": "Google 搜索"},
{ "_id": 3, "name": "Facebook", "address": "脸书"},
{ "_id": 4, "name": "Taobao", "address": "淘宝"},
{ "_id": 5, "name": "Zhihu", "address": "知乎"}
] x = mycol.insert_many(mylist) # 输出插入的所有文档对应的 _id 值
print(x.inserted_ids)
输出结果为:
[1, 2, 3, 4, 5]
执行完以上查找,我们可以在命令终端,查看数据是否已插入: