1

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"}}}}]}}
,{"$unwind":"$albums"},
{"$sort":{"albums.order":1,"created":1}},
{"$group":{"_id":"$albums.id","assets":{"$push":"$$ROOT"}}},
{"$project":{"assets":{"$slice":["$assets",6]}}}])

my asset collection having entries like.

{
    "_id": "8ed6dc473e331d895ecbdea7f9bbd55e",
    "created": 1479463428837247,
    "updated": 1479463428837247,
    "catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
    "albums": {
      "id": "18f292072b7555965e0f61a0331a3a43",
      "order": "D1"
    }
  },
  {
    "_id": "aec701d23dfe5f3a100d678e1a04f716",
    "created": 1479463428902155,
    "updated": 1479463428902155,
    "catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
    "albums": {
      "id": "18f292072b7555965e0f61a0331a3a43",
     "order": "D2"
    }
  },
  {
    "_id": "b2fe8f9dcf8f3f92f6396cfa574ef71c",
    "created": 1479463428971650,
    "updated": 1479463428971650,
    "catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
    "albums": {
      "id": "18f292072b7555965e0f61a0331a3a43",
      "order": "E1"
    }
  },
  {
    "_id": "050b550fb23bf780f93eec43cbc667f1",
    "created": 1479463428065297,
    "updated": 1479463428065297,
    "catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
    "albums": {
      "id": "78c72b85944e5085a4a3be77a2d175fe",
      "order": "D1"
    }
  },
  {
    "_id": "dc54279bc0318f41808b65d1fe7142b2",
    "created": 1479463428134560,
    "updated": 1479463428134560,
    "catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
    "albums": {
      "id": "78c72b85944e5085a4a3be77a2d175fe",
      "order": "D2"
    }
  }

my result returned is something like.

[
  {
    '_id': '18f292072b7555965e0f61a0331a3a43',
    assets: [
      {
        "_id": "8ed6dc473e331d895ecbdea7f9bbd55e",
        "created": 1479463428837247,
        "updated": 1479463428837247,
        "catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
        "albums": {
          "id": "18f292072b7555965e0f61a0331a3a43",
          "order": "D1"
        }
      },
      {
        "_id": "aec701d23dfe5f3a100d678e1a04f716",
        "created": 1479463428902155,
        "updated": 1479463428902155,
        "catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
        "albums": {
          "id": "18f292072b7555965e0f61a0331a3a43",
          "order": "D2"
        }
      },
      {
        "_id": "b2fe8f9dcf8f3f92f6396cfa574ef71c",
        "created": 1479463428971650,
        "updated": 1479463428971650,
        "catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
        "albums": {
          "id": "18f292072b7555965e0f61a0331a3a43",
          "order": "E1"
        }
      }
    ],
    {
      "_id": "78c72b85944e5085a4a3be77a2d175fe",
      "assets": [
        {
          "_id": "050b550fb23bf780f93eec43cbc667f1",
          "created": 1479463428065297,
          "updated": 1479463428065297,
          "catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
          "albums": {
            "id": "78c72b85944e5085a4a3be77a2d175fe",
            "order": "D1"
          }
        },
        {
          "_id": "dc54279bc0318f41808b65d1fe7142b2",
          "created": 1479463428134560,
          "updated": 1479463428134560,
          "catalog_id": "4b5ce056175151e3f0aa1741eedb7f12",
          "albums": {
            "id": "78c72b85944e5085a4a3be77a2d175fe",
            "order": "D2"
          }
        }
      ]
    }
  ]

result 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. Any idea

4
  • Why don't you reverse the result array after aggregation? Commented Nov 23, 2016 at 5:43
  • can do it , it will add one more array operation overhead in my server code. Commented Nov 23, 2016 at 9:37
  • Please tell us what you are trying to do. Perhaps there are better way to do this and get the expected result. Commented Nov 23, 2016 at 9:58
  • @Styvane basically I have an asset collection in mongoDB. Now an asset can be a part of multiple albums , we do store this detail in Asset document as an array of albums. Now I am trying to list the assets which belong the the given album_ids. But limiting the result also , sorting it based on order. Commented Nov 23, 2016 at 13:12

1 Answer 1

2

I know this is really late but I found this looking it up myself and went through around 25 stack overflow questions so I figured I would post. Here is what I did (its for a chatroom so if you want to paginate you want to in reverse order.

                        Group.find({ name: { $regex : new RegExp(name, "i") } })
                            .skip(pageOptions.page*pageOptions.limit)
                            .limit(pageOptions.limit)
                            .sort( '-createdAt' )
                            .populate("sender")
                            .exec((err, result) => {
                                result.reverse();
                                callback(err, result);
                            });

The key was:

result.reverse();

Hope this can help someone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.