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] } Copy
// 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] }
await collections.insertOne({ arr: [1, 2, 3, 4, 5] });// Return [1, 2]await collections.findOne({}, { projection: { arr: { $slice: 2 }, },});// Return [3, 4]await collections.findOne({}, { projection: { arr: { $slice: [-3, 2] }, },}); Copy
await collections.insertOne({ arr: [1, 2, 3, 4, 5] });// Return [1, 2]await collections.findOne({}, { projection: { arr: { $slice: 2 }, },});// Return [3, 4]await collections.findOne({}, { projection: { arr: { $slice: [-3, 2] }, },});
Either of the following:
Specifies the number of elements in an array to return in the query result.
Has one of the following forms:
Example