1

i want to paging in structure. params are id = 558186caa6df1a2194422949 and Question is = 55818779a6df1a219442294d and get first 2 rows in Question datasource.

How do I resolve it?

Below is 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"
}

1 Answer 1

2

The challenge is that regular find() operators on arrays that yield a match in the array will return the whole array , so querying for Questions._Id = 55818779a6df1a219442294d will yield the whole Questions array including the second item 55818774a6df1a219442294c which you probably don't want to deal with.

You can use the aggregation pipeline and $unwind to "dig through" the arrays of arrays and then ultimately use $skip and $limit stages to achieve the pagination you seek, as follows:

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

In general, one must be careful about multiple $unwind operators because it could result in an explosion of data but here, our first $match always results in a single document because _id is unique. The second $match also seems to cut down the input set significantly so the second $unwind is probably deal with order 100 or 1000 datasource items to skip and limit, not millions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.