Skip to main content

lookup

$lookup

Uses the pipeline value as a key to look up a corresponding value from the argument collection. This is the inverse of $get: the input value is the key, the argument is the collection.

Returns undefined if the key is not found in the collection.

Parameters

  • Object collection (object, required): The key/value lookup table.

Example

// Map a string value to a numeric code
new Schema('string').transformer({$lookup: {low: 1, medium: 2, high: 3}})
// 'medium' → 2

// Use a role name to look up its permission set
new Schema('string').transformer({
$lookup: {$literal: {
admin: ['read', 'write', 'delete'],
editor: ['read', 'write'],
viewer: ['read'],
}}
})

// Validate that a key exists in the table (require a defined result)
new Schema('string').validator({
$require: {$lookup: {$literal: {us: 'United States', uk: 'United Kingdom', ca: 'Canada'}}}
})