Lookup class

$lookup aggregation stage

Stage description

Performs a left outer join to an unsharded collection in the same database to filter in documents from the “joined” collection for processing. To each input document, the $lookup stage adds a new array field whose elements are the matching documents from the “joined” collection. The $lookup stage passes these reshaped documents to the next stage.

Examples:

  1. Single Equality Join

Dart code:

Lookup(
  from: 'inventory',
  localField: 'item',
  foreignField: 'sku',
  as: 'inventory_docs'
).build()

Equivalent mongoDB aggregation stage:

{
  $lookup: {
    from: "inventory",
    localField: "item",
    foreignField: "sku",
    as: "inventory_docs"
  }
}
  1. Specify Multiple Join Conditions:

Dart code:

Lookup.withPipeline(
  from: 'warehouses',
  let: {
    'order_item': Field('item'),
    'order_qty': Field('ordered')
  },
  pipeline: [
    Match(Expr(And([
      Eq(Field('stock_item'), Var('order_item')),
      Gte(Field('instock'), Var('order_qty'))
    ]))),
    Project({
      'stock_item': 0,
      '_id': 0
    })
  ],
  as: 'stockdata'
).build()

Equivalent mongoDB aggregation stage:

{
  $lookup: {
    from: "warehouses",
    let: { order_item: "$item", order_qty: "$ordered" },
    pipeline: [
      { $match:
        { $expr:
          { $and:
            [
              { $eq: [ "$stock_item",  "$$order_item" ] },
              { $gte: [ "$instock", "$$order_qty" ] }
            ]
          }
        }
      },
      { $project: { stock_item: 0, _id: 0 } }
    ],
    as: "stockdata"
  }
}

https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

Inheritance

Constructors

Lookup({required String from, required String localField, required String foreignField, required String as})
Creates ordinary $lookup stage
Lookup.withPipeline({required String from, required Map<String, dynamic> let, required List<AggregationStage> pipeline, required String as})
Creates $lookup stage with it's own pipeline

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

build() Map<String, Object>
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited