1

Given the Schema Displayed Below & MongoDB shell version: 2.0.4:

How would I go about counting the number of items in the impressions array ?

I am thinking I have to do a Map Reduce, which seems to be a little complex, I thought that the count Function would do it.
Can someone Demonstrate this?

Mongo Schema

5
  • 1
    engage.impressions.length, but i'm pretty sure that's not what you want, but that's basically what you're asking. Commented Sep 30, 2012 at 23:00
  • 1
    assuming you want a function that maps function(doc) { return doc.impressions.length}, your best bet is the have a count.impressions attribute that you update the same time you update impressions. Commented Sep 30, 2012 at 23:02
  • db.engagement.find({company_name: "me"},{impressions:1}).length() is failing Commented Sep 30, 2012 at 23:11
  • 1
    you are trying to use mongodb synchronously. .find is an asynchronous function. Commented Sep 30, 2012 at 23:14
  • 1
    Also if you are trying to use db object in a MR it wont work. Commented Sep 30, 2012 at 23:31

1 Answer 1

1

A simple example of counting is:

var count = 0;
db.engagement.find({company_name: "me"},{impressions:1}).forEach(
    function (doc) {
        count += doc.impressions.length;
    }
)
print("Impressions: " + count);

If you have a large number of documents to process, you would be better maintaining the count as an explicit field. You could either update the count when pushing to the impressions array, or use an incremental MapReduce to re-count for updated documents as needed.

Sign up to request clarification or add additional context in comments.

6 Comments

I'd Like to push this further, perhaps I want to count by date range? Shall I just catch the time Key and check if its in the range and increment the counter only if its in range? @stennie Thank You.
Or should I open another Question?
@CAM: If you want to get fancier with the manipulation, you may want to consider using the new Aggregation Framework in MongoDB 2.2. It has built-ins operators such as $group which will let you do sums. Probably better if you can open a new question with the full example of what you are trying to achieve for the "advanced" query. NB: it would also be helpful if your time fields were Dates rather than strings.
RE: Date, I was considering that, this is just a JSON Object so I could play with how i want to store data.<br> I was reading on the Aggregation Framework - but then I have to Upgrade Mongo on all Environments, and isn't that still in Beta, or was I reading bad info?
@CAM: For comparison, here is a recent answer that has an example of doing a count in both MapReduce and Aggregation Framework: Twitter data - Finding the most mentioned user in MongoDB.
|

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.