0

Categories

    {
        "_id" : ObjectId("61740086893f048528d166b9"),
        "name": "Category1",
        "tracks" : [ 
            "61c65353565a2d9a1cd3020d", 
            "61c74518962dc3efb96c3438", 
            "61c74775703176a6f72df444"
        ]
    }

Tracks

    {
        "_id" : ObjectId("61c65353565a2d9a1cd3020d"),
        "name" : "Track1",
        "categoryId" : ObjectId("61740086893f048528d166b9"),
        "creatorId" : ObjectId("61c6478304e98ed63e8ee7d3"),
        "thumbnailId" : ObjectId("61c65353565a2d9a1cd3020c"),
        "plays" : [],
        "media" : {
            "type" : "wav",
            "url" : ""
        },
        "status" : "approved",
        "downloads" : [],
        "uploadedDate" : 1640387411
    }

Assuming that I have 5 categories and each category has many tracks ID, I wanna get N last tracks for each category so I used this code below

    categories.aggregate([
        {
            $project: {
                tracks: { $slice: ["$tracks", -2] },
            },
        },
    ]

And the response is

    [
        {
            "_id": "61740086893f048528d166b9",
            "tracks": [
                "61c74518962dc3efb96c3438",
                "61c74775703176a6f72df444"
            ]
        },
        {
            "_id": "61740094893f048528d166c1",
            "tracks": []
        },
        {
            "_id": "617400a0893f048528d166cb",
            "tracks": []
        }
    ]

So far it's good, but the question is how can I replace each category's tracks from an array of IDs to an array of objects?

I tried $loopup but I probably didn't implement the localField correctly.

Expected result

    [
        {
            "_id": "61740086893f048528d166b9",
            "tracks": [
                {
                    "_id": ObjectId("61c74518962dc3efb96c3438")
                    ...
                },
                {
                    "_id": ObjectId("61c74775703176a6f72df444")
                    ...
                }
            ]
        },
        {
            "_id": "61740094893f048528d166c1",
            "tracks": []
        },
        {
            "_id": "617400a0893f048528d166cb",
            "tracks": []
        }
    ]

***** UPDATE *****

I'm trying to replace the creatorId by createdBy which is an object of the users from the users collection

Users

    {
      "_id": ObjectId("61c6478304e98ed63e8ee7cb"),
      "email": "[email protected]",
      "username": "USER999",
      "tracks": [
        ObjectId("61c65353565a2d9a1cd3020d"),
      ],
    }

The expected result should be

    [
        {
            "_id": "61740086893f048528d166b9",
            "tracks": [
                {
                    "_id": ObjectId("61c74518962dc3efb96c3438"),
                    "createdBy": {
                        "_id": "userId"
                        ...
                    },
                    ...
                },
                {
                    "_id": ObjectId("61c74775703176a6f72df444"),
                    "createdBy": {
                        "_id": "userId"
                        ...
                    }
                    ...
                }
            ]
        },
        {
            "_id": "61740094893f048528d166c1",
            "tracks": []
        },
        {
            "_id": "617400a0893f048528d166cb",
            "tracks": []
        }
    ]

In addition to the solution below by ray, I added the code here https://mongoplayground.net/p/8AjmnL-vhtz

The createdBy is at the top level but not under every track

1 Answer 1

1

$lookup is the correct way for you to find the corresponding object in Tracks collection. Why your code does not work is that you are storing strings in tracks array in Categories collection; while the _id of Tracks collection is ObjectId. There will be no $lookup result as the datatypes do not match. What you can do is converting the strings to ObjectId by using $toObjectId in a $map, and then do the $lookup

db.categories.aggregate([
  {
    $project: {
      tracks: {
        $slice: [
          "$tracks",
          -2
        ]
      }
    }
  },
  {
    $project: {
      tracks: {
        "$map": {
          "input": "$tracks",
          "as": "t",
          "in": {
            "$toObjectId": "$$t"
          }
        }
      }
    }
  },
  {
    "$lookup": {
      "from": "tracks",
      let: {
        t: "$tracks"
      },
      pipeline: [
        {
          $match: {
            $expr: {
              "$in": [
                "$_id",
                "$$t"
              ]
            }
          }
        }
      ],
      "as": "tracks"
    }
  }
])

Here is the Mongo playground for your reference.

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

5 Comments

thanks for your help, I have one more question, each track has creatorId, how can i replace the creatorId by createdBy which is an object with the user's info? I have a collection of users
@daniel93 the idea is the same. Simply perform a $lookup to the users collection. You should be fine this time as the createdBy is already objectId type. If you need help with a concrete example, please update your question with the new sample dataset and expected output.
updated with mongodb playground link
@daniel93 try to do the $lookup in the subpipeline when $lookup tracks, like this
that's right, I looked where to perform the nested lookup tried couple of things but I missed the pipeline... was confused but I learnt something new! :)

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.