Aggregate help joining documents

25 views
Skip to first unread message

Eman Nollase

unread,
Mar 28, 2023, 9:26:30 AM3/28/23
to kmongo
Please see the code below

val nameProp = Product::name
        val descProp = Product::description
        val userProp = User::username


        val pipeline: List<Bson> = listOf(
            lookup("users", Product::user.name, User::id.name, "user"),
            unwind("\$user"),
            project(
                fields(
                    include(nameProp, descProp, userProp),
                    excludeId()
                )
            )
        )

        val products = productTable.aggregate<Document>(pipeline).toList()
        println(products)

the products always empty? any idea? thanks

Eman Nollase

unread,
Mar 28, 2023, 10:57:52 AM3/28/23
to kmongo
Hi i manage to make it work but the new problem i'm having is that  it returns all the field of the user.
How can I limit the relation field to be returned?

thanks

Teo Fonrouge

unread,
Mar 30, 2023, 11:25:22 AM3/30/23
to kmongo
Hi,

You need to use a lookup with a pipeline, and you can use a $project stage in order to filter what fields will be returned.

Check the $lookup with pipeline documentation:  https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/


best regards,

Teo

Reply all
Reply to author
Forward
0 new messages