0

I'm trying to join some items from one collection to another. The original collection has nested arrays that I'm trying to join on from the other collection.

My data looks like this:

Feed

{
    name: "feed name",
    carousels: [
        {
            name: "Top 5",
            items: [
                {
                    _id: ObjectId("id of some soundpack")
                },
                {
                    _id: ObjectId("id of some other soundpack")
                }

            ]
        },
        {
            name: "Latest",
            items: [
                {
                    _id: ObjectId("id of some soundpack")
                },
                {
                    _id: ObjectId("id of some other soundpack")
                }

            ]
        }
    ]
}

Soundpack

{
    _id: ObjectId("some object id"),
    name: "soundpack name 1",
    url: "http://www.somewebsite.com",
    image: "http://www.image.com"
},
{
    _id: ObjectId("some object other id"),
    name: "soundpack name 2",
    url: "http://www.somewebsite.com",
    image: "http://www.image.com"
},
{
    _id: ObjectId("yet another object id"),
    name: "soundpack name 3",
    url: "http://www.somewebsite.com",
    image: "http://www.image.com"
}

And my code looks like this:

fun getFeedByName(name: String): Feed? {
        return db.getCollection(FEED_COLLECTION, Feed::class.java).aggregate(listOf(
            match(Feed::name eq name),
            lookup (
                SOUNDPACK_COLLECTION,
                "carousels.items._id",
                "_id",
                "carousels.items"
            )
        )).firstOrNull()
    }

For whatever reason (I've made multiple attempts at this with no luck), the join values never seem to populate into carousels.items.

Any help here would be appreciated~

Vinnie
  • 252
  • 1
  • 9
  • Not to be "that guy", but your general approach seems to generate results via [this playground](https://mongoplayground.net/p/mwyokzuv7Xt). Does `SOUNDPACK_COLLECTION` resolve correctly? Are the `_id` values matching? As an aside, you may need a more complicated aggregation if you want to keep the the `name` information from the `carousels` – user20042973 Nov 03 '22 at 15:44
  • @user20042973 lol someone needs to "that guy"! Thanks for your response. So the fact that it's working in the playground makes me think there's something being lost in translation between the KMongo client I'm using and the actual query it's generating. One thing I noticed in KMongo is that when I specify applying the joined data in "carousel.items", it's not putting them in the "items" field in the "carousel" object. It's creating a new literal field "carousel.items" on the root feed object. – Vinnie Nov 03 '22 at 19:47
  • @user20042973 ok so I just noticed an issue in your playground which is probably the same issue i'm having. When putting info in the `carousels` field, it's not putting an array of carousel data, it's putting an individual object. So presumably the first set of matches. Ideally, it should be joining a soundpack to each item id in each carousel in the feed. – Vinnie Nov 03 '22 at 20:36
  • Right, that is the way that `$lookup` is going to function when a nested field is used for the `as` field name. Said another way, and similar to other stages like `$addFields`, the `$lookup` is going to overwrite the existing field. That's why I mentioned that you may need a more complicated aggregation (specifically one that "zips" the output of the `$lookup` with the existing source data in a subsequent `$addFields` stage). But that consideration is (probably) separate to the claim that _the join values never seem to populate into `carousels.items`_ Are you getting any output currently? – user20042973 Nov 04 '22 at 17:07

0 Answers0