Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-51121

$graphqLookup with dynamic value evaluation in restrictSearchWithMatch expression

    • Type: Icon: Question Question
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Aggregation Framework
    • None

      Here is the example 

      hdbrs:PRIMARY> db.models.find()
      { "_id" : ObjectId("5f6c72cec48056980f22d3d0"), "id" : "a1", "refs" : [ { "id" : "b1" }, { "id" : "c1" }, { "id" : "d1" }, { "id" : "e1" } ] }
      { "_id" : ObjectId("5f6c72e3c48056980f22d3d1"), "id" : "c1", "refs" : [ { "id" : "c11" }, { "id" : "c1" }, { "id" : "d1" }, { "id" : "c12" }, { "id" : "c13" } ] }
      { "_id" : ObjectId("5f6c72f2c48056980f22d3d2"), "id" : "b1", "refs" : [ { "id" : "b11" }, { "id" : "b12" }, { "id" : "b13" } ] }
      { "_id" : ObjectId("5f6c72ffc48056980f22d3d3"), "id" : "d1", "refs" : [ { "id" : "d11" }, { "id" : "d12" }, { "id" : "d13" } ] }
      { "_id" : ObjectId("5f6c78d7c48056980f22d3d4"), "id" : "c11", "refs" : [ { "id" : "c21" }, { "id" : "c22" } ] }
      

      In the above example, when we search for the "c12" and "d13" id and all of it's parents using aggregation.

      hdbrs:PRIMARY> db.models.aggregate([{$match: {"refs.id": {$in: searchIds}}}, {$graphLookup: {from: "models", startWith: searchIds, connectFromField: "id", connectToField: "refs.id", as: "Tree", maxDepth: 2, depthField: "depth" }}, {$project: {"Tree.id": 1, "Tree.depth": 1, _id: 0}}], {explain: false})
      
      // Results 
      
      { "Tree" : [ { "id" : "c1", "depth" : NumberLong(1) }, { "id" : "a1", "depth" : NumberLong(1) }, { "id" : "c11", "depth" : NumberLong(0) }, { "id" : "d1", "depth" : NumberLong(0) } ] }
      { "Tree" : [ { "id" : "a1", "depth" : NumberLong(1) }, { "id" : "c1", "depth" : NumberLong(1) }, { "id" : "d1", "depth" : NumberLong(0) }, { "id" : "c11", "depth" : NumberLong(0) } ] }
      

      As per the results you can see both documents contain the same document ids. If we allow dynamic search on the value "Tree.id" where the condition

      {restrictSearchWithMatch: {$nin: "Tree.id"}}

      will stop the repeated documents from getting again into the results set and instead of the different documents from the result set can we get output something like a single document as result set.

            Assignee:
            edwin.zhou@mongodb.com Edwin Zhou
            Reporter:
            hitesh.baldaniya@contentstack.com Hitesh Baldaniya
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: