Articles → MONGODB → $Slice Operator In Mongodb Aggregation Framework

$Slice Operator In Mongodb Aggregation Framework






Purpose





Syntax


{
  $project: {
    fieldName: {
      $slice: [arrayField, numberOfElements]
    }
  }
}



Example




Picture showing the sample mongodb collection




db.sales.aggregate([{
    $project: {
        slicedOrders: {
            $slice: ["$orders", 1]
        }
    }
}])



Output


Picture showing the output of $slice operator in mongodb





Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, March 19, 2024

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250