clangers
clangers

Reputation: 576

Group query upon nested object in mongodb

I am looking at performing a group by upon a given value within a nest object. For example, my document structure is as follows:

{ 
  "ip_address": "192.168.132.12",
  "timestamp" : "2014-08-28T06:41:24",
  "response" : 200,
  "uri": {
     "term": "value A",
     "page" : "1",
     "category" : "category 1"
  }
}

What I am looking at achieving is performing a group aggregate upon the uri.term field. I know how this is achieved for a direct field, e.g. "ip_address":

db.search_stb.aggregate({ $group : {_id : "$ip_address", total : { $sum : 1 }} });

but I'm completely stuck on how to do this for the nested object.

Any help would be much appreciated!

Upvotes: 3

Views: 7520

Answers (1)

vmr
vmr

Reputation: 1935

You just need to use the '.' operator as follows:

db.search_stb.aggregate({ $group : {_id : "$uri.term", total : { $sum : 1 }} });

Upvotes: 4

Related Questions