📜  Python MongoDB – insert_many 查询

📅  最后修改于: 2022-05-13 01:54:27.806000             🧑  作者: Mango

Python MongoDB – insert_many 查询

MongoDB是一个跨平台的面向文档和非关系(即 NoSQL)的数据库程序。它是一个开源文档数据库,以键值对的形式存储数据。 MongoDB 由 MongoDB Inc. 开发,最初于 2009 年 2 月 11 日发布。它是用 C++、Go、JavaScript、 Python语言编写的。 MongoDB 提供高速、高可用性和高可扩展性。

insert_many()

此方法用于在 MongoDB 中的集合或数据库中插入多个条目。此方法的参数是一个列表,其中包含我们要插入到集合中的数据的字典。
这个方法返回一个类“~pymongo.results.InsertManyResult”的实例,它有一个“_id”字段,保存插入文档的id。如果文档没有指定“_id”字段,那么 MongoDB 将在列表中的所有数据中添加“_id”字段,并在插入之前为文档分配唯一的对象 id。

示例 1:在此示例中,提供了 _id。

Python3
# importing Mongoclient from pymongo
from pymongo import MongoClient
 
 
myclient = MongoClient("mongodb://localhost:27017/")
 
# database
db = myclient["GFG"]
 
# Created or Switched to collection
# names: GeeksForGeeks
collection = db["Student"]
 
# Creating a list of records which we
# insert in the collection using the
# update_many() method.
mylist = [
  { "_id": 1, "name": "Vishwash", "Roll No": "1001", "Branch":"CSE"},
  { "_id": 2, "name": "Vishesh", "Roll No": "1002", "Branch":"IT"},
  { "_id": 3, "name": "Shivam", "Roll No": "1003", "Branch":"ME"},
  { "_id": 4, "name": "Yash", "Roll No": "1004", "Branch":"ECE"},
]
 
# In the above list _id field is provided so it inserted in
# the collection as specified.
 
# Inserting the entire list in the collection
collection.insert_many(mylist)


Python3
# importing Mongoclient from pymongo
from pymongo import MongoClient
 
 
myclient = MongoClient("mongodb://localhost:27017/")
 
# database
db = myclient["GFG"]
 
# Created or Switched to collection
# names: GeeksForGeeks
collection = db["Geeks"]
 
# Creating a list of records which we
# insert in the collection using the
# update_many() method.
mylist = [
  {"Manufacturer":"Honda", "Model":"City", "Color":"Black"},
  {"Manufacturer":"Tata", "Model":"Altroz", "Color":"Golden"},
  {"Manufacturer":"Honda", "Model":"Civic", "Color":"Red"},
  {"Manufacturer":"Hyundai", "Model":"i20", "Color":"white"},
  {"Manufacturer":"Maruti", "Model":"Swift", "Color":"Blue"},
]
# In the above list we do not specify the _id, the MongoDB assigns
# a unique id to all the records in the collection by default.
 
# Inserting the entire list in the collection
collection.insert_many(mylist)


输出:

python-mongodb-插入-许多

示例 2:本示例中没有提供 _id,它由 MongoDB 自动分配。

Python3

# importing Mongoclient from pymongo
from pymongo import MongoClient
 
 
myclient = MongoClient("mongodb://localhost:27017/")
 
# database
db = myclient["GFG"]
 
# Created or Switched to collection
# names: GeeksForGeeks
collection = db["Geeks"]
 
# Creating a list of records which we
# insert in the collection using the
# update_many() method.
mylist = [
  {"Manufacturer":"Honda", "Model":"City", "Color":"Black"},
  {"Manufacturer":"Tata", "Model":"Altroz", "Color":"Golden"},
  {"Manufacturer":"Honda", "Model":"Civic", "Color":"Red"},
  {"Manufacturer":"Hyundai", "Model":"i20", "Color":"white"},
  {"Manufacturer":"Maruti", "Model":"Swift", "Color":"Blue"},
]
# In the above list we do not specify the _id, the MongoDB assigns
# a unique id to all the records in the collection by default.
 
# Inserting the entire list in the collection
collection.insert_many(mylist)

输出 :

python-mongodb-insert-many-2