My logic in function does not allow to duplicate data in normal operation {"name":"paul", "age":"21"} {"name":"goerge", "age":"21"} {"name":"paul", "age":"44"} {"name":"paul", "age":"21"}
``My function does not allow last one as paul and age 21 is already in database. But when the traffic is high, it is found that that one is also added. To solve this problem, I used compound index as db.test.createIndex({"name":1, "age":1}, {unique:true}). This does not let to allow another data starting with paul.
in a wrong way by first doing a find on { paul , 21 } and then do a insert if none is found. As you found out a { paul , 21 } can be added between your find and your insert.
Having a unique index like you did is a sure way to prevent that. However, your logic is still wrong and you will still try to create a duplicate { paul , 21 } but then your code will generate an duplicate key error.