Ranjeet
Ranjeet

Reputation: 8072

MongoDb $slice not working

I am trying to fetch some records using aggregate function in MongoDB, but it show following invalid operator $slice:

db.getCollection('test').aggregate( [
    { $match: { 'subjectId': '123' } }, 
    { $sort: { 'assessmentDate': -1 } }, 
    { $group: { '_id': '$area', 'docs': { $push: "$$ROOT" } } },  
    { $project: { docs: { $slice: ["$docs", 1, 1]   }  }  }, 
])

Error("Printing Stack Trace")@:0()@src/mongo/shell/utils.js:37([objectArray])@src/mongo/shell/collection.js:866
@(shell):1
uncaught exception: aggregate failed: {
    "errmsg" : "exception: invalid operator '$slice'",
    "code" : 15999,
    "ok" : 0
}

MongoDB version 3.0.9

Upvotes: 5

Views: 1363

Answers (1)

Ranjeet
Ranjeet

Reputation: 8072

To use $slice we should use mongoDB version above 3.2.x, Change log can be found on changelog.

Documentation for $slice documentation

Upvotes: 2

Related Questions