SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)

    MongoDB Aggregate()

    Looks like a project of mine will be utilizing MongoDB, and this is just a whole new ball game to me... I cant for the life of me get my head around the aggregate function, all the examples I find seem to be off my learning style.

    Assuming I have SOME_ID and SOME_COUNT in a "document" or a row, how would I write the equivelent:

    Code:
    select
    some_id, sum(some_count)
    from
    myCollection
    group by
    some_id

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    After taking lunch I tried again with success:

    Code:
    db.myCollection.aggregate([{$group: {_id: "$some_id", count: {$sum: 1}}}]);
    I don't fully understand it but hey.. Now I need to figure out how to add in $where...

  3. #3
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    71 Post(s)
    Tagged
    0 Thread(s)
    I.. think (and i could be wrong - it's been all of 30 seconds reading the aggregation documentation) that it would be something along the lines of...

    Code:
    db.myCollection.aggregate({$match: {field : "value"}},{$group: {_id: "$some_id", count: {$sum: 1}}});
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Yeah I got it figured out, I'll put some examples here later. My biggest problem was the syntax as I had never seen JSON style before and if you don't know it, it can be very hard to stop problems.

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,136
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Not sure if this would be of any help, but I used to use http://jsonviewer.codeplex.com/ when I needed to verify the json I received or created was valid. Maybe there is something better for MongoDB out there, but if not, this will provide some help.

  6. #6
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    65 Post(s)
    Tagged
    2 Thread(s)
    Ah cool thank you! I think I'm getting the hang of it, and it looks like JSON / Mongo might be my life for awhile


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •