Hi,
I have a scenario in which i need to update "count" field and "lastAccessDate" for a document when a user visits.
Old Scenario :
That works fine as it has if-else condition.
db.userFrequency.update({"host" : "
abc.com","sessionId" : "ff8378ed-ccda-4a75-b24b-4a4bb1153e39"},{"$set" : {"lastAccessDate":"20140825"},"$inc" : {"count":1}},{upsert:true})
Now, my requirement has changed,I need to update count like below
New Scenario :
obj = db.userFrequency.update({"host" : "
abc.com","sessionId" : "ff8378ed-ccda-4a75-b24b-4a4bb1153e39"});
if(obj!=null){
// Increment Count only if a visitor hasn't visit today.
if(obj.get("lastAccessDate" != todayDate))
{
db.userFrequency.update({"host" : "
abc.com","sessionId" : "ff8378ed-ccda-4a75-b24b-4a4bb1153e39"},{"$set" : {"lastAccessDate": todayDate},"$inc" : {"count":1}},{upsert:true});
}
// If an user has already visited today, do nothing.
else{
}
}else{
// Insert the document as it doesnt exists in collection.
db.userFrequency.update({"host" : "
abc.com","sessionId" : "ff8378ed-ccda-4a75-b24b-4a4bb1153e39"},{"$set" : {"lastAccessDate": todayDate},"$inc" : {"count":1}},{upsert:true});
}
My Problem :
Previously, in old scenario, i can do operation in a single query but in new scenario, i am not able to do it in single query but in update with upsert option, there is only if {where clause}-else{update} condition is possible.
Please, suggest any way so i can do the new scenario in just a single db query because my data is in GB's, i cant afford 2 queries.
Thanks,
Deepesh