سؤال

I want to record user events to my mongodb collection. Is it possible to set up a simple query to only store the last recorded time stamp for an event for an arbitrary, dynamically changing set of events (so when a new event is received, it is inserted)?

I know the following doesn't work, but i wanted to give an idea of what I'm thinking:

uievents.update({_id:id},{
    $set: {userName:user,
                   ("events."+eventName): {
                eventName: eventName,
                serverTime: curTime,
                browserTime: btime,
                value: value
            }},{$upsert:true});
هل كانت مفيدة؟

المحلول

Easiest way to do his is really simple:

query = {$set: {}}
query["$set"]["event."+eventName] = {
                eventName: eventName,
                serverTime: curTime,
                browserTime: btime,
                value: value
            }

uievents.update({_id:id},query,{$upsert:true});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top