Re: Mongodb sort inner array of outside doc

190 views
Skip to first unread message

Ronald Stalder

unread,
Mar 13, 2013, 4:35:14 PM3/13/13
to mongod...@googlegroups.com
Are "service" and "apps" arrays ? In your example they are objects {} - not arrays []. If they're objects you cannot unwind them, just use dot notation: $unwind "service.apps.updates" - then sort on "service.apps.updates.date" should work

Cheers
Ronald

Richard Hartmann

unread,
Mar 14, 2013, 11:33:37 AM3/14/13
to mongod...@googlegroups.com
So the actual schema is an array of services, with an array of apps, with an array of updates. I wanted to order the updates and group them together in a single apps object because I didn't simply wants hundreds of updates because of unwinding the updates... so heres what the shell ended up like (I'm using abbreviations and a larger schema, I just wanted to give this as an example to work with):

db.servers.aggregate( {$unwind:'$service'}, {$unwind:'$service.apps'}, {$unwind: '$service.apps.updates'}, {$sort:{'service.apps.updates.date':-1}}, {$group: {_id: '$_id', 'updates': {$push: '$service.apps.updates'}}}, {$project: {'service.apps.updates':'$updates'}} );

I thought this was an interesting thing to dive into and quite useful
Reply all
Reply to author
Forward
0 new messages