I am using an upsert query to insert/update a document in a collection, however the query not always reliably upserts. It sometimes inserts another document when it should have updated. You can see the query below - Updated query after Alex pointed it out -
db.test.update(
{ departmentID: "1",
storeID: "1",
customerID: "1"},
{
$set: {
name: "Andy",
rating: 1,
score: 1
},
$setOnInsert: {
departmentID: "1",
storeID: "1",
customerID: "1"
}
},
{ upsert: true }
);
This query mostly works, but sometimes what ends up happening is when the above query is run in quick succession(gap of 0.004 seconds), the query ends up inserting 2 documents with departmentID-1, storeID-1 and customerID-1, whereas it should have inserted only one document and updated on the second instance.