10

Gurus - I'm stuck in a situation that I can't figure out how I can query from the following collection "users", it has 2 embedded documents "signup" and "activity":

{
    "appid": 2,
    "userid": 404915,
    "signup": {
        "dt": "2010-12-28",
        "platform": 2 
    },
    "activity": {
        {
            "dt": "2010-12-28",
            "platform": 3,
            "login_count": 8,
            "game_completed": 13 
        },
        {
            "dt": "2010-12-30",
            "platform": 3,
            "login_count": 8,
            "game_completed": 13 
        } ,
        {
            "dt": "2010-12-31",
            "platform": 3,
            "login_count": 8,
            "game_completed": 13 
        } 
    }
},{"appid":2,"userid":404915...}

I need to query:

unique logins of users who signed up between Date and Date+7 and logged in within Date

Then:

Unique logins of users who signed up between Date and Date+7, and logged in between Date+7 and Date+14

PLEASE PLEASE Guide me how I can achieve this any example/sample? based on this will be really helpful :-)

Thanks a lot!

Rex Logan
  • 26,248
  • 10
  • 35
  • 48

1 Answers1

8

Here is how you get the result for your first query:

var start = new Date(2010, 11, 25);
var end = new Date(2010, 12, 1);

db.users.distinct("userid", {"signup.dt" : {$gte: start, $lte: end},
      "activity" : {"$elemMatch" : { dt: {$gte: start, $lte: end}}}});

The second is like it with adding 7 days to the start and end date to the dates after activity.

Jeff the Bear
  • 5,603
  • 3
  • 22
  • 18
  • One more problem here, I looked at RockMongo, there are keys added after adding an activity sub-document: "activity": { "0": {"dt": "2010-12-30"...},"1": {"dt": "2010-12-29"...} } ... will the above query worked on this? – Syed Kamran Haider Nov 26 '10 at 13:16
  • I don't think it will work if the sub-document is structured that way. It should work if the keys are in the sub-document: "activity": [ { "key" : "0", "dt": "2010-12-30"...}, { "key" : "1", "dt": "2010-12-29"...} ] ... – Jeff the Bear Nov 26 '10 at 19:19