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

Projection form of $slice only works with find, not with $project

    • Type: Icon: Bug Bug
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • Query Execution
    • ALL
    • Hide
      Enterprise test> db.test.find()
      [ { _id: 1, a: [ 1, 2, 3, 4, 5 ] } ]
      Enterprise test> db.test.find({}, { a : { $slice : [0, 3] } })
      [ { _id: 1, a: [ 1, 2, 3 ] } ]
      Enterprise test> db.test.aggregate([{ $project : { a : { $slice : [0, 5 ] } } }])
      MongoServerError: Failed to optimize pipeline :: caused by :: First argument to $slice must be an array, but is of type: int
      Enterprise test> 
      Show
      Enterprise test> db.test.find() [ { _id: 1, a: [ 1, 2, 3, 4, 5 ] } ] Enterprise test> db.test.find({}, { a : { $slice : [0, 3] } }) [ { _id: 1, a: [ 1, 2, 3 ] } ] Enterprise test> db.test.aggregate([{ $project : { a : { $slice : [0, 5 ] } } }]) MongoServerError: Failed to optimize pipeline :: caused by :: First argument to $slice must be an array, but is of type: int Enterprise test>

      The projection form of $slice:

      https://www.mongodb.com/docs/manual/reference/operator/projection/slice/

      only works with find, not with $project.

      I understand there is another form of $slice:

      https://www.mongodb.com/docs/manual/reference/operator/aggregation/slice/

      But the issue here is why the first form doesn't work with $project.

            Assignee:
            david.storch@mongodb.com David Storch
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: