I have the following mongo document representing website and links found on it.
{"website":
"id":"123456",
"links":[
"link1","link2","link3",..."link1000"
]
}
and I want to return a limited number of links from that specific document found by id, in order to support pagination, e.g. page=1, limit=100 should return first 100 links. I tried something like this, but it doesn't work. Is there any way I can limit the number of subdocuments?
String query="{'website.id:'"+id+"'}";
String fields="{links:1}";
DBCursor dbCursor=this.getCollection(crawlersCollection)
.find(this.query(query),this.query(fields)).skip(skip).limit(limit);
The other approach I tried is to use $slice
String fields="{'links':{'$slice':["+skip+","+limit+"]}}";
but this doesn't work well, because it's not very useful for pagination. For example, if I have 43 links, page=2 with limit=100 will return 43 results, so I have to check the number of links before creating a query.
Do you have any suggestions how to solve this issue?
Thanks