Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get paginated/sliced data of subdocument array in mongo collection?

Tags:

mongodb

I have a collection like this:

{
"_id" : ObjectId("51f4ad560364f5490ccebe26"),
"fiTpcs" : [
    "uuid1",
    "uuid2",
    "uuid3",
    "uuid4",
    "uuid5"
],
"fiTpcsCnt" : 5
}

The list of fiTpcs is long and can go to hundreds later. When I retrieve my collection, I want to get a limited list of fiTpcs, say 20 at a time and fire separate requests to get subsequent data from fiTpcs. I just want to ensure that the queries don't get slow later when I have a lot more data. Is there a way to do it in mongodb? until now, I have been doing

db.userext.find({"_id" : ObjectId("51f4ad560364f5490ccebe26")}).pretty();

which always gets me the full fiTpcs array. I am using java driver with Spring and a solution using Spring/java would also be fine. Please note - if the solution requires mongo to scan through the whole fiTpcs array and then slice a part of it, it doesn't really add any performance benefits, that is not what I am looking for.

like image 843
Jayz Avatar asked Aug 10 '13 01:08

Jayz


People also ask

How do I query an array value in MongoDB?

To query if the array field contains at least one element with the specified value, use the filter { <field>: <value> } where <value> is the element value. To specify conditions on the elements in the array field, use query operators in the query filter document: { <array field>: { <operator1>: <value1>, ... } }

How does pagination work in MongoDB?

In MongoDB, the pagination phenomenon is used to get an output that can be easily understandable. Pagination is a procedure to display large disarranged output in a page format. With the help of pagination, the result can be retrieved faster as compared to the general methods of MongoDB.

How do you find documents with a matching item in an embedded array?

Use $match With $eq to Find Matching Documents in an Array in MongoDB. Use $match With $all to Find Matching Documents in an Array in MongoDB.

How do I query embedded files in MongoDB?

Accessing embedded/nested documents – In MongoDB, you can access the fields of nested/embedded documents of the collection using dot notation and when you are using dot notation, then the field and the nested field must be inside the quotation marks.


1 Answers

I may not understand your question in full depth, but seems like $slice is the droid your are looking for:

> db.page.find()
{ "_id" : ObjectId("51f4ad560364f5490ccebe26"), "fiTpcs" : [ "uuid1", "uuid2", "uuid3", "uuid4", "uuid5" ], "fiTpcsCnt" : 2 }
> db.page.find({}, {"fiTpcs" : {$slice : 3}})
{ "_id" : ObjectId("51f4ad560364f5490ccebe26"), "fiTpcs" : [ "uuid1", "uuid2", "uuid3" ], "fiTpcsCnt" : 2 }
> db.page.find({}, {"fiTpcs" : {$slice : [1,3]}})
{ "_id" : ObjectId("51f4ad560364f5490ccebe26"), "fiTpcs" : [ "uuid2", "uuid3", "uuid4" ], "fiTpcsCnt" : 2 }
like image 197
madhead - StandWithUkraine Avatar answered Nov 15 '22 06:11

madhead - StandWithUkraine