Limit number of values in a field using MongoDB

空扰寡人 提交于 2019-12-11 01:58:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!