Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-53743

mongodb $slice aggregate operator issue when third argument is 0 { $slice: [ <array>, <position>, <n> ] }

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Works as Designed
    • Icon: Major - P3 Major - P3
    • None
    • None
    • None
    • None

    Description

      Problem Description

      Consider the below Mongodb Query.
       

      {{  db.groups.aggregate([{        $match: {        _id: {            $in: [220]
              },        active: true        }
          },
          {        $project: {        myArr: {            $slice: ["$arr", -1, 0]
              },        name: 1                }
          }]).pretty()}}
      

      When I execute this query I encounter error:
       

      {{2020-12-05T19:16:43.002-0500 E  QUERY    [js] uncaught exception: Error: command failed: {    "operationTime" : Timestamp(1607213793, 1),    "ok" : 0,    "errmsg" : "Third argument to $slice must be positive: 0",    "code" : 28729,    "codeName" : "Location28729",    "$clusterTime" : {        "clusterTime" : Timestamp(1607213793, 1),        "signature" : {            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),            "keyId" : NumberLong(0)
              }
          }
      } : aggregate failed :}}
      

      As per the $slice documentation, https://docs.mongodb.com/manual/reference/operator/aggregation/slice/ the third argument ('n') to the $slice aggregate operator cannot be negative if the second argument 'position' is specified. I calculate the third argument value to 0 which means, I expect 0 array elements as the result. However, I encounter errmsg" : "Third argument to $slice must be positive: 0".

      Please suggest if my understanding is incorrect as the query doesn't execute as I expect.

      Steps to Reproduce

       
      {{  db.groups.aggregate([{        $match: {        _id: {            $in: [220]
              },        active: true        }
          },
          {        $project: {        myArr: {            $slice: ["$arr", -1, 0]
              },        name: 1                }
          }]).pretty()}}
      

      Expected Results

      Expecting myArr should be empty array.

      Actual Results

      "errmsg" : "Third argument to $slice must be positive: 0",

      Additional Notes

      Attachments

        Activity

          People

            edwin.zhou@mongodb.com Edwin Zhou
            javatutorials2016@gmail.com John Anto
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: