Skip to content Skip to sidebar Skip to footer

Limit Number Of Values In A Field Using Mongodb

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 wo

Solution 1:

You need to use the $slice operator.

db.collection.find( { /* filter */ }, { "array": { "$slice": -3 } } );

Post a Comment for "Limit Number Of Values In A Field Using Mongodb"