Dynamic Frequency Map from MongoDB Keys

I am using MiniMongo through Meteor, and I am trying to create a frequency table based on a dynamic set of queries.

I have two main fields: localHourand localDay. I expect a lot of overlaps, and I would like to determine where most overlaps occur. My current way of doing it is this.

if(TempStats.findOne({
            localHour: hours,
            localDay: day
          })){//checks if there is already some entry on the same day/hour

            TempStats.update({//if so, we just increment frequency
              localHour: hours,
              localDay: day
            },{
              $inc: {freq: 1}
            })

          } else {//if nothing exists yet, we put in a new entry

            TempStats.insert({
              localHour: hours,
              localDay: day,
              freq: 1
            });

          }

Essentially, this code runs every time I have new data that I want to insert. It works fine at the moment, so after entering all the data, I can sort by frequency to find the most frequent number of hours and days ( TempStats.find({}, {sort: {freq: -1}}).fetch()).

. , , , . , , . Mongo ( MiniMongo) ?

!

+4
3

.

-, 2 . . , Date. , , , . . , , ( ) . . , , - , . TempMapCollection, . forEach() ( , ..).

javascript. , , EJSON. , , .

- :

TempMapCollection.find().forEach(function(doc) {
    var date = doc.dateTime.getDate();
    TempReduceCollection.upsert({timequery: hours}, {$inc: {freq: 1}});
})

. , , 2 .

+1

, miniMongo , . - ( - -1). , . dbs.

@nickmilon, upsert $inc .

+3
  • : , else, , , upsert = true, $inc freq 1 :
  • for alternative ways of counting frequencies: if you store the date as a datetime object, I would suggest using aggregation (I'm not sure that they added aggregation support back in minimongo), but there is, then with aggregation you can use datetime operators like $ hour, $ week etc. for filtering and counting $ count, to count frequencies without having to keep counts in the database.
+2
source

All Articles