mongodb - Sort subfields with unknown parent -
i have schema this:
{ "_id" : "555", "connections" : [ { "id" : 111 "time" : 1456439249 }, { "id" : 222 "time" : 1556412345 } ... } "users" : [ "111" : { "id" : 111 "name" : "michael" }, "222" : { "id" : 222 "name" : "jim" } ... }
i want sorted connections time , users data.
i'm trying this:
db.getcollection('mycollecion') .find( {'_id' : '555'}, {'_id' : 0, 'connections' : 1, 'users' : 1} ).sort({'connections.time' : 1})
probably "connections.time" not correct path, because it's array.
how can sort connections subfield "time"?
and possible in same query filter "users" appearing on connections ids?
please try through .aggregation()
> db.mycollecion.aggregate([ {$unwind: '$connections'}, {$sort: {'connections.time': 1}}, {$group: { _id: '$_id', connections: {$push: '$connections'} } }]);
Comments
Post a Comment