lookup static method

Map<String, Object> lookup({
  1. required String from,
  2. required String localField,
  3. String foreignField = '_id',
  4. String? as,
})

Constructs a MongoDB $lookup aggregation stage for joining collections.

The $lookup stage performs a left outer join to an unsharded collection in the same database to filter in documents from the "joined" collection.

from The target collection to join with localField The field from the input documents foreignField The field from the documents of the "from" collection (defaults to '_id') as The name of the output array field (defaults to '${from}_info')

Example:

var query = DQ.lookup(
  from: 'users',
  localField: 'user_id',
  foreignField: '_id',
  as: 'user_data'
);
// { '$lookup': { 'from': 'users', 'localField': 'user_id', 'foreignField': '_id', 'as': 'user_data' } }

Implementation

static Map<String, Object> lookup({
  required String from,
  required String localField,
  String foreignField = '_id',
  String? as,
}) {
  as ??= '${from}_info';

  return {
    '\$lookup': {
      'from': from,
      'localField': localField,
      'foreignField': foreignField,
      'as': as,
    }
  };
}