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?
Asked
Active
Viewed 409 times
1
-
1`engage.impressions.length`, but i'm pretty sure that's not what you want, but that's basically what you're asking. – Jonathan Ong Sep 30 '12 at 23:00
-
1assuming 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. – Jonathan Ong Sep 30 '12 at 23:02
-
db.engagement.find({company_name: "me"},{impressions:1}).length() is failing – CAM Sep 30 '12 at 23:11
-
1you are trying to use mongodb synchronously. `.find` is an asynchronous function. – Jonathan Ong Sep 30 '12 at 23:14
-
1Also if you are trying to use db object in a MR it wont work. – Sammaye Sep 30 '12 at 23:31
1 Answers
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.

Stennie
- 63,885
- 14
- 149
- 175
-
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. – CAM Oct 01 '12 at 01:43
-
-
1@CAM: If you want to get fancier with the manipulation, you may want to consider using the new [Aggregation Framework](http://docs.mongodb.org/manual/applications/aggregation/) in MongoDB 2.2. It has built-ins operators such as [$group](http://docs.mongodb.org/manual/reference/aggregation/group/#_S_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. – Stennie Oct 01 '12 at 01:57
-
RE: Date, I was considering that, this is just a JSON Object so I could play with how i want to store data.
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 Oct 01 '12 at 02:12 -
@CAM: the Aggregation Framework is included in the MongoDB 2.2.0 production release (end of August, 2012) so is no longer in beta. Definitely worth having a play with; you may find the "pipeline" approach more intuitive than Map/Reduce. If you want to remain on MongoDB 2.0.x you should consider updating to the latest production release (currently 2.0.7; you mention using 2.0.4 in the original question). – Stennie Oct 01 '12 at 02:41
-
1@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](http://stackoverflow.com/questions/12654451). – Stennie Oct 01 '12 at 02:46