Sharded view with lookup and graphlookup

Hi! As part of a PoC, I did some experimentation on a very simple local cluster (sharded, two nodes with some zones contraints). Everything’s going fine, but I’ve got a gap between my experiment and the documentation concerning sharded views. The documentation (6.0+) states:

Views are considered sharded if their underlying collection is sharded. You cannot specify a sharded view for the from field in $lookup and $graphLookup operations.

However, it works perfectly on my test example (a sharded collection groups and a simple view groupsView that only contains a sort stage).

I was wondering if this was an omission linked to 5.1 release, which allows to use sharded collections from lookup and graphlookup? Or maybe my test case is too simplistic? If I can provide you with more details, please don’t hesitate to ask.


MongoDB version: 6.0.5
Sharded view:

db.createView(
  "groupsView",
  "groups",
  [{ $sort: { name: 1 } }]
)

Execution stats :

{
    "serverInfo" : {
        "host" : "b68e294df2ba",
        "port" : 27017.0,
        "version" : "6.0.5",
        "gitVersion" : "c9a99c120371d4d4c52cbb15dac34a36ce8d3b1d"
    },
    "serverParameters" : {
        "internalQueryFacetBufferSizeBytes" : 104857600.0,
        "internalQueryFacetMaxOutputDocSizeBytes" : 104857600.0,
        "internalLookupStageIntermediateDocumentMaxSizeBytes" : 104857600.0,
        "internalDocumentSourceGroupMaxMemoryBytes" : 104857600.0,
        "internalQueryMaxBlockingSortMemoryUsageBytes" : 104857600.0,
        "internalQueryProhibitBlockingMergeOnMongoS" : 0.0,
        "internalQueryMaxAddToSetBytes" : 104857600.0,
        "internalDocumentSourceSetWindowFieldsMaxMemoryBytes" : 104857600.0
    },
    "mergeType" : "mongos",
    "splitPipeline" : {
        "shardsPart" : [
            {
                "$graphLookup" : {
                    "from" : "groupsView",
                    "as" : "toto",
                    "connectToField" : "group_index",
                    "connectFromField" : "group_index",
                    "startWith" : "$group_index"
                }
            }
        ],
        "mergerPart" : [
            {
                "$mergeCursors" : {
                    "lsid" : {
                        "id" : "e3671c9d-6a7b-4c22-9d53-8708bd4a1299",
                        "uid" : "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NM.. 8 more bytes"
                    },
                    "compareWholeSortKey" : false,
                    "tailableMode" : "normal",
                    "nss" : "pocDB.groups",
                    "allowPartialResults" : false,
                    "recordRemoteOpWaitTime" : false
                }
            }
        ]
    },
    "shards" : {
        "rs-shard-eu-frc-2" : {
            "host" : "mongodb-shard-eu-frc-2-001:27017",
            "stages" : [
                {
                    "$cursor" : {
                        "queryPlanner" : {
                            "namespace" : "pocDB.groups",
                            "indexFilterSet" : false,
                            "parsedQuery" : {

                            },
                            "queryHash" : "17830885",
                            "planCacheKey" : "17830885",
                            "maxIndexedOrSolutionsReached" : false,
                            "maxIndexedAndSolutionsReached" : false,
                            "maxScansToExplodeReached" : false,
                            "winningPlan" : {
                                "stage" : "SHARDING_FILTER",
                                "inputStage" : {
                                    "stage" : "COLLSCAN",
                                    "direction" : "forward"
                                }
                            },
                            "rejectedPlans" : [

                            ]
                        },
                        "executionStats" : {
                            "executionSuccess" : true,
                            "nReturned" : 10000.0,
                            "executionTimeMillis" : 24023.0,
                            "totalKeysExamined" : 0.0,
                            "totalDocsExamined" : 10000.0,
                            "executionStages" : {
                                "stage" : "SHARDING_FILTER",
                                "nReturned" : 10000.0,
                                "executionTimeMillisEstimate" : 1.0,
                                "works" : 10002.0,
                                "advanced" : 10000.0,
                                "needTime" : 1.0,
                                "needYield" : 0.0,
                                "saveState" : 11.0,
                                "restoreState" : 11.0,
                                "isEOF" : 1.0,
                                "chunkSkips" : 0.0,
                                "inputStage" : {
                                    "stage" : "COLLSCAN",
                                    "nReturned" : 10000.0,
                                    "executionTimeMillisEstimate" : 1.0,
                                    "works" : 10002.0,
                                    "advanced" : 10000.0,
                                    "needTime" : 1.0,
                                    "needYield" : 0.0,
                                    "saveState" : 11.0,
                                    "restoreState" : 11.0,
                                    "isEOF" : 1.0,
                                    "direction" : "forward",
                                    "docsExamined" : 10000.0
                                }
                            }
                        }
                    },
                    "nReturned" : 10000,
                    "executionTimeMillisEstimate" : 17
                },
                {
                    "$graphLookup" : {
                        "from" : "groupsView",
                        "as" : "toto",
                        "connectToField" : "group_index",
                        "connectFromField" : "group_index",
                        "startWith" : "$group_index"
                    },
                    "nReturned" : 10000,
                    "executionTimeMillisEstimate" : 24024
                }
            ]
        }
    },
    "command" : {
        "aggregate" : "groups",
        "pipeline" : [
            {
                "$graphLookup" : {
                    "from" : "groupsView",
                    "startWith" : "$group_index",
                    "connectFromField" : "group_index",
                    "connectToField" : "group_index",
                    "as" : "toto"
                }
            }
        ],
        "cursor" : {

        }
    },
    "ok" : 1.0,
    "$clusterTime" : {
        "clusterTime" : "2023-06-05T09:08:26.000+0000",
        "signature" : {
            "hash" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=",
            "keyId" : 0
        }
    },
    "operationTime" : "2023-06-05T09:08:26.000+0000"
}
1 Like

Hi @Remi_Delmas and welcome to MongoDB community forums!!

As mentioned in the SERVER-27533: Allow “from” collection of $graphLookup to be sharded and SERVER-29159: Allow “from” collection of $lookup to be sharded, “from” collections in $lookup and $graphLookup is introduced in the 5.1 release.

While I appreciate your feedback on the documentation, I am raising an internal ticket in response to the same.

Let us know if you have any further concerns.

Regards
Aasawari

2 Likes