unwind static method

Map<String, Object> unwind({
  1. required String path,
  2. String? as,
  3. bool? preserveNullAndEmptyArrays,
})

Constructs a MongoDB $unwind aggregation stage for deconstructing array fields.

The $unwind stage deconstructs an array field from the input documents to output a document for each element. Each output document is the input document with the value of the array field replaced by the element.

path The field path to an array field (will be prefixed with $) as Optional name for the index field (when preserving empty arrays) preserveNullAndEmptyArrays If true, includes documents with null, missing, or empty arrays

Example:

// Simple unwind
var query = DQ.unwind(path: 'items');
// { '$unwind': '$items' }

// Unwind with preserve null arrays
var query = DQ.unwind(path: 'tags', preserveNullAndEmptyArrays: true);
// { '$unwind': { 'path': '$tags', 'preserveNullAndEmptyArrays': true } }

Implementation

static Map<String, Object> unwind({
  required String path,
  String? as,
  bool? preserveNullAndEmptyArrays,
}) {
  if (as == null && preserveNullAndEmptyArrays == null) {
    return {'\$unwind': "\$$path"};
  }

  return {
    '\$unwind': {
      'path': "\$$path",
      if (preserveNullAndEmptyArrays != null)
        'preserveNullAndEmptyArrays': preserveNullAndEmptyArrays,
      if (as != null) 'as': as
    }
  };
}