My aggregation pipeline on mongoDB is like.
db.assets.aggregate([{"$match":{"$or":[{"albums":{"$elemMatch":{"id":"78c72b85944e5085a4a3be77a2d175fe","order":{"$gt":"C2"}}}},{"albums":{"$elemMatch":{"id":"18f292072b7555965e0f61a0331a3a43","order":{"$gt":"C2"}}}}]}}
,
,
{"$sort":{"albums.order":1,"created":1}},
{"$group":{"_id":"$albums.id","assets":
}},
{"$project":{"assets":
}}])
my asset collection having entries like.
{
"_id": "8ed6dc473e331d895ecbdea7f9bbd55e",
"created": 1479463428837247,
"updated": 1479463428837247,
"catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
"albums":[
,]
},
{
"_id": "aec701d23dfe5f3a100d678e1a04f716",
"created": 1479463428902155,
"updated": 1479463428902155,
"catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
"albums": [
,]
},
{
"_id": "b2fe8f9dcf8f3f92f6396cfa574ef71c",
"created": 1479463428971650,
"updated": 1479463428971650,
"catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
"albums": [
,]
},
{
"_id": "050b550fb23bf780f93eec43cbc667f1",
"created": 1479463428065297,
"updated": 1479463428065297,
"catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
"albums":[
,]
},
{
"_id": "dc54279bc0318f41808b65d1fe7142b2",
"created": 1479463428134560,
"updated": 1479463428134560,
"catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
"albums":[
,]
}
my result returned is something like.
[
{
'_id': '18f292072b7555965e0f61a0331a3a43',
assets: [
{
"_id": "8ed6dc473e331d895ecbdea7f9bbd55e",
"created": 1479463428837247,
"updated": 1479463428837247,
"catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
"albums":
},
{
"_id": "aec701d23dfe5f3a100d678e1a04f716",
"created": 1479463428902155,
"updated": 1479463428902155,
"catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
"albums":
},
{
"_id": "b2fe8f9dcf8f3f92f6396cfa574ef71c",
"created": 1479463428971650,
"updated": 1479463428971650,
"catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
"albums":
}
],
{
"_id": "78c72b85944e5085a4a3be77a2d175fe",
"assets": [
{
"_id": "050b550fb23bf780f93eec43cbc667f1",
"created": 1479463428065297,
"updated": 1479463428065297,
"catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
"albums":
},
{
"_id": "dc54279bc0318f41808b65d1fe7142b2",
"created": 1479463428134560,
"updated": 1479463428134560,
"catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
"albums":
}
]
}
]
result returned from the query cursor is not in the same order for album as passed in the match query with or. 78c72b85944e5085a4a3be77a2d175fe is coming after 18f292072b7555965e0f61a0331a3a43. ideally it should be in the same order it is passed to the query.