python - mongoimport 显示错误:E11000 重复键错误
问题描述
我正在做一个关于 Flask 的教程。我正在尝试将 json 文件中的一些信息添加到 MongoDB 数据库中
用户.json
[{"id":1,"first_name":"Jacques","last_name":"Blazewicz","email":"jblazewicz0@posterous.com","password":"k9doaly"},
{"id":2,"first_name":"Budd","last_name":"Zellick","email":"bzellick1@uol.com.br","password":"kik8N0cyKG"},
{"id":3,"first_name":"Simone","last_name":"Brenston","email":"sbrenston2@squarespace.com","password":"9BO7nEvdci8"},
{"id":4,"first_name":"Waneta","last_name":"Stading","email":"wstading3@google.es","password":"GpzWY536X"},
{"id":5,"first_name":"Barbey","last_name":"Corder","email":"bcorder4@csmonitor.com","password":"BXFkbgEz"},
{"id":6,"first_name":"Becca","last_name":"Hartington","email":"bhartington5@wsj.com","password":"ijh3RfxcGB"},
{"id":7,"first_name":"Elyse","last_name":"Eddy","email":"eeddy6@archive.org","password":"QJHg5Gc0V"},
{"id":8,"first_name":"Reggie","last_name":"Souster","email":"rsouster7@4shared.com","password":"nmCeQGRC"},
{"id":9,"first_name":"Brnaby","last_name":"Abrahmson","email":"babrahmson8@digg.com","password":"BqOgwfIMJmTx"},
{"id":10,"first_name":"Yuma","last_name":"Graine","email":"ygraine9@tmall.com","password":"4VpkAWyL"},
{"id":11,"first_name":"Quinton","last_name":"Chater","email":"qchatera@squarespace.com","password":"7x6IYp"},
{"id":12,"first_name":"Raymund","last_name":"Moorman","email":"rmoormanb@indiatimes.com","password":"aRgaXm1"},
{"id":13,"first_name":"Boycey","last_name":"Ferrelli","email":"bferrellic@cnet.com","password":"VCKtIJEUi"},
{"id":14,"first_name":"Ty","last_name":"Raffeorty","email":"traffeortyd@cnet.com","password":"wVOd2oi"},
{"id":15,"first_name":"Judie","last_name":"Penella","email":"jpenellae@psu.edu","password":"0WP0F6516"},
{"id":16,"first_name":"Alden","last_name":"Gemlett","email":"agemlettf@independent.co.uk","password":"3rAW4wlTsCWz"},
{"id":17,"first_name":"Josephina","last_name":"MacLeod","email":"jmacleodg@livejournal.com","password":"gQ7ytj7CtA6"},
{"id":18,"first_name":"Lorene","last_name":"Lindeberg","email":"llindebergh@chron.com","password":"Q4WU8aHkm"},
{"id":19,"first_name":"Lamar","last_name":"Sotham","email":"lsothami@rediff.com","password":"5yyhnYPTDs"},
{"id":20,"first_name":"Derek","last_name":"Breakey","email":"dbreakeyj@home.pl","password":"jrz2kl2jeX"}]
我在终端中运行这个命令:
mongoimport --jsonArray --db UTA_Enrollment --collection user --file users.json
我没有获取数据库中的所有 20 行,而是只获取第一行,并且这些错误消息显示在命令行中。
2021-02-25T17:47:40.654-0800 connected to: mongodb://localhost/
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 continuing through error: E11000 duplicate key error collection: UTA_Enrollment.user index: user_id_1 dup key: { user_id: null }
2021-02-25T17:47:40.658-0800 1 document(s) imported successfully. 19 document(s) failed to import.
我之前曾在 python 中运行过这些代码行,并且已经从数据库中删除了已注释的两个条目。
class User(db.Document):
user_id = db.IntField( unique=True )
first_name = db.StringField( max_length=50 )
last_name = db.StringField( max_length=50 )
email = db.StringField( max_length=30 )
password = db.StringField( max_length=30 )
@app.route("/user")
def user():
# User(user_id=1, first_name="Frank", last_name="Ho", email="frank.ho@uta.com", password="password123").save()
# User(user_id=2, first_name="Mary", last_name="Jane", email="mary.jane@uta.com", password="abc1234").save()
users = User.objects.all()
return render_template("user.html", users=users)
我不知道为什么会这样,因为每个条目的 id 都不同。谢谢您的帮助
解决方案
user_id
似乎有一个索引强制字段值是唯一的
但是在您的 users.json 文件中,没有user_id
字段。因此,在导入数据时,它会为所有用户创建具有空值的字段。并且由于该领域,因为是唯一的,所以您有此错误dup key:
因此,要解决此问题,您必须删除 user_id 字段上的索引。文档的标识符_id
在 mongodb中命名
推荐阅读
- django - 关系“weather_city”不存在
- rabbitmq - 一条消息处理正常,下一条消息被拒绝并进入死信
- c# - StreamReader 似乎在 ReadLine() 之后前进,即使没有什么可读的
- powermockito - PowerMockito 无法创建具有匿名块的类的实例
- sqlite-net - c# SqliteConnection 为数据库设置密码
- oracle - Oracle Internet Directory 12.2.1.3.0 执行调用时发生错误:域的钱包创建失败
- html - 未找到与 HTML 位于同一文件夹中的 CSS 文件
- python - 不兼容的形状:Tensorflow/Keras Sequential LSTM with Autoencoder
- javascript - 数组和检查里面的值的问题
- python - 使用 playsound + os 路径时出现错误 python