How can paging in nested array Mongodb -


i want paging in structure. params id = 558186caa6df1a2194422949 , question = 55818779a6df1a219442294d , first 2 rows in question datasource.

how resolve it?

below mongodb document structure:

/* 1 */ {     "_id" : objectid("558186caa6df1a2194422949"),     "questions" : [          {             "_id" : objectid("55818779a6df1a219442294d"),             "datasource" : [                  {                     "_id" : objectid("55818796a6df1a2194422950"),                     "text" : "q1-1",                     "value" : "q1-1"                 },                  {                     "_id" : objectid("5581879aa6df1a2194422951"),                     "text" : "q1-2",                     "value" : "q1-2"                 },                  {                     "_id" : objectid("5581879ea6df1a2194422952"),                     "text" : "q1-3",                     "value" : "q1-3"                 }             ]         },          {             "_id" : objectid("55818774a6df1a219442294c"),             "datasource" : [                  {                     "_id" : objectid("55818788a6df1a219442294e"),                     "text" : "q2-1",                     "value" : "q2-1"                 },                  {                     "_id" : objectid("5581878fa6df1a219442294f"),                     "text" : "q2-2",                     "value" : "q2-2"                 }             ]         }     ],     "name" : "test" } 

the challenge regular find() operators on arrays yield match in array return whole array , querying questions._id = 55818779a6df1a219442294d yield whole questions array including second item 55818774a6df1a219442294c don't want deal with.

you can use aggregation pipeline , $unwind "dig through" arrays of arrays , use $skip , $limit stages achieve pagination seek, follows:

db.foo.aggregate([ {$match: { "_id": objectid("558186caa6df1a2194422949") }} ,{$unwind: "$questions"} ,{$match: { "questions._id": objectid("55818779a6df1a219442294d") }}   ,{$unwind: "$questions.datasource"} ,{$skip: 1} ,{$limit: 2}                   ]); 

Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -