3

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!

1
  • Did you try {_id: "$uri.term"} ? Commented Sep 3, 2014 at 13:55

1 Answer 1

4

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

db.search_stb.aggregate({ $group : {_id : "$uri.term", total : { $sum : 1 }} });
Sign up to request clarification or add additional context in comments.

2 Comments

thanks - I feel really stupid as it was such a simple answer! Another question, as the uri.term is optional, how do I get the aggregate to exclude or ignore null values?
Np, exclude not null would be another question, check questions with the same tag , you will surely find the right answer.. :) Accept the answer if it useful so that it will act as a reference for others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.