dimanche 8 mars 2015

Inserting Documents fail due to duplicate key generation(usecase: login and logout insert on browser refresh)

The following code logs a document on user connection and user disconnect. If the user refreshes the page, then the disconnect followed by connect happens quickly and the same objectid that is generated at the disconnect is generated at the connect.



function handleSocket(socket) {
socket.notify_user_state_data.state = "CONNECTED";
notify_user_state_collection.insert(
socket.notify_user_state_data, function(err) {});

socket.on('disconnect', function () {
socket.notify_user_state_data.state = 'DISCONNECTED';
notify_user_state_collection.insert(
socket.notify_user_state_data, function(err) {});
}


This might be due to the warning mentioned in the documention http://ift.tt/1EnJxKw


The relationship between the order of ObjectId values and generation time is not strict within a single second


How to solve this issue ?


Aucun commentaire:

Enregistrer un commentaire