[SERVER-4899] aggregation: need an expression to give the size of an array in projections Created: 07/Feb/12  Updated: 27/Oct/15  Resolved: 09/Sep/13

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 2.5.3

Type: New Feature Priority: Major - P3
Reporter: Daniel Pasette (Inactive) Assignee: Matt Dannenberg
Resolution: Done Votes: 21
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by DOCS-1949 Document: aggregation: need an expres... Closed
Related
related to SERVER-8582 Extend document expression language i... Closed
is related to SERVER-447 new aggregation framework Closed
Backwards Compatibility: Fully Compatible
Participants:

 Description   

Some analytics queries can't be done by the aggregation framework only for the lack of an array size operator:

var a1 = db.runCommand({ aggregate:"track", pipeline:[
    {$group: {
        _id: "$urllower",
        visits: {$sum: "$visits"},
        kw: {$addToSet: "$kw"}
    },
     {$project: {
         urllower: "$_id",
         visits: 1,
         keywords: {$size: "$kw"}
     }
]});

The request here is for the $size operator at the end of the projection, which would give the size of the keyword array being built up here.

I choose $size for the name because we already use that for the same meaning here:
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24size .



 Comments   
Comment by Asya Kamsky [ 15/Nov/13 ]

Note, this cannot be used to determine if the field is an array.

Just like $unwind, this operator will give an error if applied to a non-array.

Possibly related: SERVER-8088

Comment by auto [ 09/Sep/13 ]

Author:

{u'username': u'dannenberg', u'name': u'matt dannenberg', u'email': u'matt.dannenberg@10gen.com'}

Message: SERVER-4899 $size for aggregation
Branch: master
https://github.com/mongodb/mongo/commit/abb240549a0174f52d1ee18a8866e0ad65a10881

Comment by Ryan Behan [ 26/Aug/13 ]

+1 This would be very useful for our data.

Comment by David Henderson [ 23/Jul/13 ]

This would be very useful - just coded an alternative using $unwind and it is slow.

Comment by Dan [ 17/Jun/13 ]

+1 . Any reason there's no progress although this was coded a year ago? Doing $unwind can have a big performance hit for big arrays

Comment by Ajay Sabhaney [ 09/May/13 ]

+1

Comment by Bjørn Stachmann [ 07/May/13 ]

+1 that would be extremely useful for analytics

Comment by Alvin Richards (Inactive) [ 20/Dec/12 ]

The workaround would be to do the following right now

{ $unwind: "$myarray"},
{ $group: { _id: {mykey: "$mykey"}, array_count: { "$sum": 1 } } }

Comment by Adam [ 05/Dec/12 ]

+1 for this feature

Comment by Nicklas Overgaard [ 20/Nov/12 ]

Hi, any reason this ticket is "stranded"?
I have some queries where I would really benefit from having this $size operator.

Comment by Jared Rosoff [ 17/Feb/12 ]

https://github.com/mongodb/mongo/pull/178

Comment by Jared Rosoff [ 10/Feb/12 ]

I have this in a branch ready to go when we want it merged.

Generated at Thu Feb 08 03:07:18 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.