Ticket: User Management

I’m trying to run the test “testLogin()”, but I always receive the same error:

com.mongodb.MongoWriteException: E11000 duplicate key error collection: mflix.sessions index: user_id_1 dup key: { : “gryffindor@hogwarts.edu” }

By the way, the test class shouldn’t clean this “session user” from the database before this test runs? Because already exists this document in the database and the test expects create it:

boolean result = dao.createUserSession(testUser.getEmail(), jwt);
assertTrue(“Should be able to create user sesssion.”, result);

Hi @brunomathidios,

You are correct.
I’ll push a fix to the data “leftover” in the unit test.

To clear out the duplicate key index error you need to delete all “gryffindor@hogwarts.edu” email users .
You can do this by running the following command via mongo shell:

use mflix
db.users.deleteMany({ "email": “gryffindor@hogwarts.edu” }) 

We will be pushing a correction soon.

N.

Thank you Norberto! It worked!

@brunomathidios I have got the same error, but can you clarify - where should i run these commands?

Hi @jekafortuna , you should run them through your mongo shell (connected via terminal)

@brunomathidios connected to my atlas cluster?

@jekafortuna correct. Run this command

use mflix
db.users.deleteMany({ "email": “gryffindor@hogwarts.edu” })

in your mongo shell after connecting to your atlas cluster.

N.

1 Like

@Norberto @brunomathidios sorry guys, i’ve connected to my atlas cluster via terminal window, enter those commands and get a result:
MongoDB Enterprise mflix-shard-0:PRIMARY> db.users.deleteMany({“email”: “gryffindor@hogwarts.edu”})
{ “acknowledged” : true, “deletedCount” : 0 }

when i start my tests - they failed with the same error - com.mongodb.MongoWriteException: E11000 duplicate key error collection: mflix.sessions index: user_id_1 dup key: { : “gryffindor@hogwarts.edu” }

1 Like

The Session object being generated in UserTest.java has a user_id that is the generated User object’s email. The duplicate error is occuring because the user_id is hardcoded as this and it is not being deleted from the database during tearDownClass().

You should run the following from your shell to rollback the state of your database to before the unit test:

db.sessions.deleteMany({ "user_id": "gryffindor@hogwarts.edu" })

and you should add the following to tearDownClass() so that the error doesn’t anymore occur during future tests:

db.getCollection("sessions").deleteOne( new Document("user_id", email ));

Hope this was helpful!

1 Like

Thanks @pppeck313 ! It helps me!