Return the first two events
Start with the synthetic case records. Open keplergrid_case_lab.cases in Documents and select this case in Filter.
{ "_id": "C-001" }
Open Sort & projection, paste this into Projection, and select Run.
{ "events": { "$slice": 2 } }
In JSON view, events contains ["received", "triaged"]. MongoDB keeps the array's stored order. The count refers to elements within this one document.
Return the last two events
Replace the projection with this object and run again. The array now shows ["assigned", "notified"].
{ "events": { "$slice": -2 } }
Skip the first event and return the next two
{ "events": { "$slice": [1, 2] } }
The first number is how many elements to skip. The second number is how many to return and must be positive in this form. This example returns ["triaged", "assigned"].
Run again with the full array
Clear Projection and run the same filter again. All four events are present. KeplerGrid opens projected results in read-only form. The same $slice name also exists in aggregation expressions, where the syntax includes the input array.
See how to include or exclude fields with projection or filter documents by array length.