onEach
open fun <Key> onEach(localField: Field<LocalDocument, Collection<Key>>, foreignField: Field<ForeignDocument, Key>)(source)
open fun <Key> onEach(localField: KProperty1<LocalDocument, Collection<Key>>, foreignField: Field<ForeignDocument, Key>)(source)
open fun <Key> onEach(localField: Field<LocalDocument, Collection<Key>>, foreignField: KProperty1<ForeignDocument, Key>)(source)
open fun <Key> onEach(localField: KProperty1<LocalDocument, Collection<Key>>, foreignField: KProperty1<ForeignDocument, Key>)(source)
Specifies that the field foreignField in the foreign collection must have a value that is strictly equal to one of the elements of the local array 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 departmentIds: List<ObjectId>,
val departments: List<Department>,
)
users.aggregate()
.lookup {
into(User::departments)
from(departments.aggregate())
onEach(User::departmentIds, Department::_id)
}Content copied to clipboard
The on operator is semantically equivalent to a match stage using a let binding:
users.aggregate()
.lookup {
into(User::departments)
val departmentIds = let(User::departmentIds)
from(
departments.aggregate()
.matchExpr { Department::_id isOneOf departmentIds }
)
}Content copied to clipboard