Skip and Limit on nested array element

北战南征 提交于 2020-04-10 03:21:27

问题


I want to apply skip and limit for paging in nested array of a document how can I perform this [Efficient Way]

My Document recored like

{
   "_id":"",
   "name":"",
   "ObjectArray":[{
       "url":"",
       "value":""
   }]
}

I want to retrieve multiple document and every document contain 'n' number of record.

I am using $in in find query to retrieve multiple record on basis of _id but how can i get certain number of element of ObjectArray in every document?


回答1:


You can try like this -

db.collection.find({}, {ObjectArray:{$slice:[0, 3]}})

This will provide you records from 0..3

$slice:[SKIP_VALUE, LIMIT_VALUE]}

For your example:-

db.collection.find({"_id":""}, {ObjectArray:{$slice:[0, 3]}})

Here is the reference for MongoDB Slice feature. http://docs.mongodb.org/manual/reference/operator/projection/slice/



来源:https://stackoverflow.com/questions/30222469/skip-and-limit-on-nested-array-element

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