on

abstract fun <Key> on(localField: Field<LocalDocument, Key>, foreignField: Field<ForeignDocument, Key>)(source)
open fun <Key> on(localField: Field<LocalDocument, Key>, foreignField: KProperty1<ForeignDocument, Key>)(source)
open fun <Key> on(localField: KProperty1<LocalDocument, Key>, foreignField: Field<ForeignDocument, Key>)(source)
open fun <Key> on(localField: KProperty1<LocalDocument, Key>, foreignField: KProperty1<ForeignDocument, Key>)(source)

Specifies that the field foreignField in the foreign collection must have a value that is strictly equal to the value of the local field localField.

To learn more about the $lookup stage, see HasLookup.lookup.

Example

class Department(
val _id: ObjectId,
val name: String,
)

class User(
val _id: ObjectId,
val name: String,
val department: ObjectId,
val departments: List<Department>,
)

users.aggregate()
.lookup {
into(User::departments)
from(departments.aggregate())
on(User::department, Department::_id)
}

The on operator is semantically equivalent to a match stage using a let binding:

users.aggregate()
.lookup {
into(User::departments)
val department = let(User::department)
from(
departments.aggregate()
.matchExpr { Department::_id eq department }
)
}

External resources