Python mongoimport显示错误:E11000重复密钥错误

Python mongoimport显示错误:E11000重复密钥错误,python,json,mongodb,flask,Python,Json,Mongodb,Flask,我正在做一个关于烧瓶的教程。我试图将json文件中的一些信息添加到MongoDB数据库中 users.json [{"id":1,"first_name":"Jacques","last_name":"Blazewicz","email":"jblazewicz0@posterous.com","password":"k9doa

我正在做一个关于烧瓶的教程。我试图将json文件中的一些信息添加到MongoDB数据库中

users.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都不同。感谢您的帮助

用户id
似乎有一个索引,强制字段值是唯一的

但是在users.json文件中,没有
user\u id
字段。因此,在导入数据时,它会为所有用户创建一个空值字段。由于该字段是唯一的,因此出现以下错误
dup键:

所以要解决这个问题,您必须删除user_id字段上的索引。
文档的标识符在mongodb中命名为
\u id
E11000重复密钥错误集合:UTA\u注册。用户索引:用户id\u 1 dup密钥:{user\u id:null}
-您是否检查了
用户id
字段上的索引类型?您可以在
mongo
shell中使用
db.collection.getIndexes()
方法列出集合的所有索引。另外,请参见。@prasad_uu当我输入db.user.getIndexes(“user_id”)时,我得到了这个
uncaught异常:错误:listIndexes失败:{“确定”:0,“errmsg”:“没有这样的命令:'0'”,“代码”:59,“代码名”:“CommandNotFound”} : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DBCollection.prototype。getIndexes@src/mongo/shell/collection.js:841:15@(shell):1:1
我对这个主题完全陌生。在mongo db shell中输入db.collection.dropIndex(user),然后在python代码中将user_id变量更改为_id?我如何将提供给我的.json文件的预期主字段作为id,并使用user\u id作为mongodb将_id解释为主键的变量名。您可以使用以下命令删除索引:
db.user.dropIndex(“user\u id”)
。无需在代码中添加
\u id
,此字段是隐式声明的。对于Json文件,我认为您需要将“id”重命名为“_id”``{“ok”:0,“errmsg”:“ns not found test.User”,“code”:26,“codeName”:“NamespaceNotFound”}``现在,当我在shell中输入
dropIndex()
方法时,就会显示这一点。还删除了user_id字段,并在上面输入相同的
mongoimport
代码,但仍然会得到重复的错误。不要紧,如果已经创建了集合,则会出现错误。我只需从头开始键入
mongoimport
,而没有创建用户集合,就解决了这个问题。谢谢你的帮助