Interface ProjectionSlice

Specifies the number of elements in an array to return in the query result.

Has one of the following forms:

// Return the first two elements
{ $slice: 2 }

// Return the last two elements
{ $slice: -2 }

// Skip 4 elements (from 0th index), return the next 2
{ $slice: [4, 2] }

// Skip backward 4 elements, return next 2 elements (forward)
{ $slice: [-4, 2] }

Example

await collection.insertOne({ arr: [1, 2, 3, 4, 5] });

// Return [1, 2]
await collection.findOne({}, {
  projection: {
  arr: { $slice: 2 },
  },
});

// Return [3, 4]
await collection.findOne({}, {
  projection: {
  arr: { $slice: [-3, 2] },
  },
});
interface ProjectionSlice {
    $slice: number | [number, number];
}

Properties

Properties

$slice: number | [number, number]

Either of the following:

  • A positive integer to return the first N elements
  • A negative integer to return the last N elements
  • A tuple of two integers to skip the first N elements and return the next M elements