为什么使用 python 控制台时 Pymongo returns E11000 重复错误
Why do Pymongo returns E11000 duplicate error when using python console
我正在学习 Pymongo documentation
中的教程
import pymongo
from pymongo import MongoClient
import datetime
client = MongoClient("mongodb://localhost:27017/")
test_db = client.test_db #This creates a database
posts = test_db.posts #This creates a collection of documents
post = {"author": "Doug",
"text": "My first blog post!",
"tags": ["mongodb", "python"],
"date": datetime.datetime.utcnow()}
post_id = posts.insert_one(post).inserted_id
代码在两种情况下都有效:在 IDE 中按 运行 或在 python 控制台中逐行按 运行。但是,每当我在 python 控制台中 运行 它时,它都会给我
pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: text_database.another_collection index: _id_ dup key: { _id: ObjectId('5f505d1e233d210283dd4632') }, full error: {'index': 0, 'code': 11000, 'keyPattern': {'_id': 1}, 'keyValue': {'_id': ObjectId('5f505d1e233d210283dd4632')}, 'errmsg': "E11000 duplicate key error collection: text_database.another_collection index: _id_ dup key: { _id: ObjectId('5f505d1e233d210283dd4632') }"}
错误。除了duplicate error之外,新建一个空集合插入文档都是正常的。请问为什么?
在 insert_one()
之后,您的 post
词典将如下所示
>>>print(post)
{'_id': ObjectId('5f506285b54093f4b9202abe'),
'author': 'Doug',
'date': datetime.datetime(2020, 9, 3, 3, 27, 1, 729287),
'tags': ['mongodb', 'python'],
'text': 'My first blog post!'}
现在您尝试使用相同的 post
再次插入它会抛出错误,因为现在它有一个 _id
字段。
如果您不想在插入后更新字典。您可以使用字典的副本插入。您可以使用 dict(post)
或 post.copy()
创建字典副本
现在您可以按如下方式更改插入内容
post_id = posts.insert_one(dict(post)).inserted_id
或
post_id = posts.insert_one(post.copy()).inserted_id
我正在学习 Pymongo documentation
中的教程import pymongo
from pymongo import MongoClient
import datetime
client = MongoClient("mongodb://localhost:27017/")
test_db = client.test_db #This creates a database
posts = test_db.posts #This creates a collection of documents
post = {"author": "Doug",
"text": "My first blog post!",
"tags": ["mongodb", "python"],
"date": datetime.datetime.utcnow()}
post_id = posts.insert_one(post).inserted_id
代码在两种情况下都有效:在 IDE 中按 运行 或在 python 控制台中逐行按 运行。但是,每当我在 python 控制台中 运行 它时,它都会给我
pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: text_database.another_collection index: _id_ dup key: { _id: ObjectId('5f505d1e233d210283dd4632') }, full error: {'index': 0, 'code': 11000, 'keyPattern': {'_id': 1}, 'keyValue': {'_id': ObjectId('5f505d1e233d210283dd4632')}, 'errmsg': "E11000 duplicate key error collection: text_database.another_collection index: _id_ dup key: { _id: ObjectId('5f505d1e233d210283dd4632') }"}
错误。除了duplicate error之外,新建一个空集合插入文档都是正常的。请问为什么?
在 insert_one()
之后,您的 post
词典将如下所示
>>>print(post)
{'_id': ObjectId('5f506285b54093f4b9202abe'),
'author': 'Doug',
'date': datetime.datetime(2020, 9, 3, 3, 27, 1, 729287),
'tags': ['mongodb', 'python'],
'text': 'My first blog post!'}
现在您尝试使用相同的 post
再次插入它会抛出错误,因为现在它有一个 _id
字段。
如果您不想在插入后更新字典。您可以使用字典的副本插入。您可以使用 dict(post)
或 post.copy()
现在您可以按如下方式更改插入内容
post_id = posts.insert_one(dict(post)).inserted_id
或
post_id = posts.insert_one(post.copy()).inserted_id