- Collect all
refs path
marked as aggregatable
- Build
$lookup
options
- Prepare
from
by pluralize path ref value
- Prepare
as
by singularize path name
- Use path name as
localField
- Use
_id
as foreignField
- Generate
unwind
options to set single document on path
- Set
preserveNullAndEmptyArrays
to true
- Set
path
same as derived as
const Person = new Schema({
sister: { type: ObjectId, ref: 'Person', aggregatable: true }
});
const Person = new Schema({
sister: {
type: ObjectId,
ref: 'Person',
aggregatable: {
from: 'users',
localField: 'sister',
foreignField: '_id',
as: 'sister'
}
}
});
Person.lookup((error, people) => { ... });
//=> { sister: { _id: ... } };
- Collect all
refs path
marked as aggregatable
- Build
$lookup
options
- Prepare
from
by pluralize path ref value
- Prepare
as
by pluralize path name
- Use path name as
localField
- Use
_id
as foreignField
- If
unwind
enabled
- Generate
unwind
options to set documents on path
- Set
preserveNullAndEmptyArrays
to true
- Set
path
as singularized as
const Person = new Schema({
relatives: { type: [ObjectId], ref: 'Person', aggregatable: true }
});
// or
const Person = new Schema({
relatives: [{ type: ObjectId, ref: 'Person', aggregatable: true }]
});
const Person = new Schema({
relatives: { type: [ObjectId], ref: 'Person', aggregatable: {unwind: true } }
});
const Person = new Schema({
relatives: {
type: [ObjectId],
ref: 'Person',
aggregatable: {
from: 'users',
localField: 'sister',
foreignField: '_id',
as: 'sister'
}
}
});
const Person = new Schema({
relatives: {
type: [ObjectId],
ref: 'Person',
aggregatable: {
from: 'users',
localField: 'sister',
foreignField: '_id',
as: 'sister',
unwind: true
}
}
});
- Without unwind - Useful if you dont want to use
populate
Person.lookup((error, people) => { ... });
//=> { bothers: [{ _id: ... }] };
- With unwind - Useful for analytics
Person.lookup((error, people) => { ... });
//=> [{ bother: { _id: ... } }, { bother: { _id: ... } }, ...];