Struggling with Syntax for $slice and $size

Hi All,
I was hoping for some help with the $slice and $size operators. Honestly, Im not sure exactly how to use them with the ‘computing fields’ lab in m121. Ive managed the $match and $project sections but not sure where to go from there. Any help would be appreciated. Thanks.


Please go through the mongodb reference documents once again.
$size - Matches any array with the number of elements specified by the argument. Ex : if you pass {$size:1} it matches only the documents containing one array element only.
$slice - Returns a subset of an array field.

Hope its help you understand by going through the mongodb reference links too …

Im still not sure i understand. Doesnt the title take a string value? I think I’m struggling with the syntax


First of all lets point out only on how operator works in filtering using Find({}):

db.collection.find( { fieldName: { $size: 2 } } ); – > So, this command only check for the array field with 2 elements. For ex: it returns

{ fieldName: [ blue, red ] }
{ fieldName: [ red, green ] }

but not : { fieldName: [ white ] } or { fieldName: [ orange, lemon, yellow ] }.

$slice: limits the number of array elements. it mainly used in update operations.

But, it used to access the subset of array from 1st and last array fields.

For Ex:
db.collection.find( {}, { arrayField: { $slice: 3 } } ) --> Here, $slice selects the first 3 items in an array in the arrayField field even though the array field as more items.
(This is the main difference between $size and $slice).

db.collection.find( {}, { arrayField: { $slice: -2 } } ) - This operation returns the last 2 items in array field.

This description is just for your understanding.But, slice still has some more information to learn while using update operations. So, you check the difference and once you have got the idea you can learn the other topics.

Coming to the “Title” field - its a string. please read the lab description properly, they have told to use the $spilt operator in particular to get the expected result.
check and rebuild your command.