问题
My collection has an array of values, but I'd like to retrieve only the last X. Example:
db.collection.find({
"array": [0,1,2,3,4,5]
})
And I'd like to query the last 3. How would I go about that?
I'm using MongoDB and this is meant for a Meteor application.
回答1:
You need to use the $slice operator.
db.collection.find( { /* filter */ }, { "array": { "$slice": -3 } } );
来源:https://stackoverflow.com/questions/34228375/limit-number-of-values-in-a-field-using-mongodb