Allow $unwind to accept expression

79 views
Skip to first unread message

Shiv

unread,
Sep 19, 2017, 8:54:47 AM9/19/17
to mongodb-user
Wondering if it is possible to allow $unwind to accept expression which resolves to array. 

Something like {$unwind:{$objectToArray:fieldpath}} which will save extra $addFields to create the array part. Similarly we can allow other array aggregation operators like $concatArrays, $set, $map and $filter operators.

Apaine

unread,
Sep 20, 2017, 10:16:33 PM9/20/17
to mongodb-user
I suggest create $project stage before with the required $addFields creating new (temp) key, then $unwind that key.

Wan Bachtiar

unread,
Sep 27, 2017, 3:30:17 AM9/27/17
to mongodb-user

Wondering if it is possible to allow $unwind to accept expression which resolves to array.

Hi Shiv,

The $unwind stage accepts a field path parameter or an expression that resolves into a field path.

As suggested by Apaine, you could try using $project first, for example:

db.collection.aggregate([{$project:{"tmp":{$objectToArray:"$foo"}}}, 
                         {$unwind:"$tmp"}
]);

Regards,
Wan.

Shiv

unread,
Sep 27, 2017, 9:21:30 AM9/27/17
to mongodb-user
Thanks Wan and Apaine for your time. I'm aware of using project/addFields stage before unwind. Actually my question( more of enhancement request) was to allow the `$unwind` stage to allow the array operators directly instead of going through extra addFields/project stage first.

Wan Bachtiar

unread,
Oct 12, 2017, 2:08:23 AM10/12/17
to mongodb-user

Actually my question( more of enhancement request)

Hi Shiv,

The idea of aggregation is to have re-usable building blocks for different operations.

Although if you feel strongly about the request, please feel free to open a SERVER ticket on MongoDB issue tracking system.
Please make sure to include a detailed use case information and example(s) in the ticket description.

Kind regards,
Wan.

Reply all
Reply to author
Forward
0 new messages