📜  Pymongo 中 insert()、insertOne() 和 insertMany() 之间的区别

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

Pymongo 中 insert()、insertOne() 和 insertMany() 之间的区别

MongoDB是一个 NoSql 数据库,可用于存储不同应用程序所需的数据。 Python可用于访问 MongoDB 数据库。 Python需要驱动程序才能访问数据库。 PyMongo 支持从Python应用程序与 MongoDB 数据库进行交互。 pymongo 包充当 MongoDB 的本机Python驱动程序。 Pymongo 提供了可在Python应用程序中使用的命令,以在 MongoDB 上执行所需的操作。 MongoDB 提供了三种将记录或文档插入数据库的方法,如下所示:

  • insert() :用于将一个或多个文档插入到集合中。如果集合不存在,则 insert() 将创建集合,然后插入指定的文档。

例子:

Python3
# importing Mongoclient from pymongo
from pymongo import MongoClient 
   
   
myclient = MongoClient("mongodb://localhost:27017/")
   
# database 
db = myclient["GFG"]
   
# Created or Switched to collection 
# names: College
collection = db["College"]
   
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"},
]
 
   
# Inserting the entire list in the collection
collection.insert(mylist)


Python3
# importing Mongoclient from pymongo
from pymongo import MongoClient
 
# Making Connection
myclient = MongoClient("mongodb://localhost:27017/")
 
# database
db = myclient["GFG"]
 
# Created or Switched to collection
# names: GeeksForGeeks
collection = db["Student"]
 
# Creating Dictionary of records to be
# inserted
record = { "_id": 5,
          "name": "Raju",
          "Roll No": "1005",
          "Branch": "CSE"}
 
 
 
# Inserting the record1 in the collection
# by using collection.insert_one()
rec_id1 = collection.insert_one(record)


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["College"]
 
mylist = [
  { "_id": 6, "name": "Deepanshu", "Roll No": "1006", "Branch":"CSE"},
  { "_id": 7, "name": "Anshul", "Roll No": "1007", "Branch":"IT"}
]
   
# Inserting the entire list in the collection
collection.insert_many(mylist)


输出:

python-mongodb-插入

  • insertOne() :用于将单个文档或记录插入数据库。如果集合不存在,则 insertOne() 方法首先创建集合,然后插入指定的文档。

注意: insertOne() 的 Pymongo 命令是 insert_one()
例子:

Python3

# importing Mongoclient from pymongo
from pymongo import MongoClient
 
# Making Connection
myclient = MongoClient("mongodb://localhost:27017/")
 
# database
db = myclient["GFG"]
 
# Created or Switched to collection
# names: GeeksForGeeks
collection = db["Student"]
 
# Creating Dictionary of records to be
# inserted
record = { "_id": 5,
          "name": "Raju",
          "Roll No": "1005",
          "Branch": "CSE"}
 
 
 
# Inserting the record1 in the collection
# by using collection.insert_one()
rec_id1 = collection.insert_one(record)

输出:

  • 插入许多()

注意: insertMany() 的 Pymongo 命令是 insert_many()
例子:

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["College"]
 
mylist = [
  { "_id": 6, "name": "Deepanshu", "Roll No": "1006", "Branch":"CSE"},
  { "_id": 7, "name": "Anshul", "Roll No": "1007", "Branch":"IT"}
]
   
# Inserting the entire list in the collection
collection.insert_many(mylist)

输出:

python-mongodb-插入-many1 .math-table { 边框折叠:折叠;宽度:100%; } .math-table td { 边框:1px 实心 #5fb962;文本对齐:左!重要;填充:8px; } .math-table th { 边框:1px 实心 #5fb962;填充:8px; } .math-table tr>th{ 背景颜色:#c6ebd9;垂直对齐:中间; } .math-table tr:nth-child(odd) { background-color: #ffffff; }

insert()insertOne()insertMany()
Pymongo equivalent command is insert()Pymongo equivalent command is insert_one()Pymongo equivalent command is insert_many()
Deprecated in newer versions of mongo engineUsed in newer versions of mongo engineUsed in newer versions of mongo engine
throws WriteResult.writeConcernError and WriteResult.writeError for write and non-write concern errors respectivelythrows either a writeError or writeConcernError exception.throws a BulkWriteError exception.
compatible with db.collection.explain()not compatible with db.collection.explain()not compatible with db.collection.explain()
If ordered is set to true and any document reports an error then the remaining documents are not inserted. If ordered is set to false then remaining documents are inserted even if an error occurs.If error is reported for the document it is not inserted into the databaseIf ordered is set to true and any document reports an error then the remaining documents are not inserted. If ordered is set to false then remaining documents are inserted even if an error occurs.
returns an object that contains the status of the operation.returns the insert_id of the document insertedreturns the insert_ids of the documents inserted