daily pastebin goal
56%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Tree Root 1 */
  2. {
  3.     "_id" : LUUID("9f3a73df-bca7-48b7-b111-285359e50a02"),
  4.     "name" : "Root 1",
  5.     "children" : [
  6.         LUUID("ca01f1ab-7c32-4e6b-a07a-e0ee9d8ec5ac"),
  7.         LUUID("6dd8c8ed-4a60-41ca-abf1-a4d795a0c213")
  8.     ]
  9. },
  10. /* Child 1 - Root 1 */
  11. {
  12.     "_id" : LUUID("ca01f1ab-7c32-4e6b-a07a-e0ee9d8ec5ac"),
  13.     "parentId" : LUUID("9f3a73df-bca7-48b7-b111-285359e50a02"),
  14.     "reference" : {
  15.         "type" : "someType",
  16.         "id" : LUUID("331503FB-C4D1-4F7A-A461-933C701EF9AB")
  17.     },
  18.     "rootReferenceId" : LUUID("9f3a73df-bca7-48b7-b111-285359e50a02"),
  19.     "rootReferenceIdAsString" : "9f3a73df-bca7-48b7-b111-285359e50a02",
  20.     "name" : "Child 1 (Root 1)"
  21. }
  22. /* Child 2 - Root 1 */
  23. {
  24.     "_id" : LUUID("6dd8c8ed-4a60-41ca-abf1-a4d795a0c213"),
  25.     "parentId" : LUUID("9f3a73df-bca7-48b7-b111-285359e50a02"),
  26.     "reference" : {
  27.         "type" : "someType",
  28.         "id" : LUUID("23E8B540-3EFB-455A-AA5C-2B67D6B59943")
  29.     },
  30.     "rootReferenceId" : LUUID("9f3a73df-bca7-48b7-b111-285359e50a02"),
  31.     "rootReferenceIdAsString" : "9f3a73df-bca7-48b7-b111-285359e50a02",
  32.     "displayName" : "Child 2 (Root 1)"
  33. }
  34. /* Tree Root 2 */
  35. {
  36.     "_id" : LUUID("27f2b4a6-5471-406a-a39b-1e0b0f8c4eb9"),
  37.     "name" : "Root 2",
  38.     "children" : [
  39.         LUUID("ad4ad076-322e-4c26-8855-91c9b1912d1f"),
  40.         LUUID("66452420-dd2f-4d27-91c9-78bd0990817c")
  41.     ]
  42. },
  43. /* Child 1 - Root 2 */
  44. {
  45.     "_id" : LUUID("ad4ad076-322e-4c26-8855-91c9b1912d1f"),
  46.     "parentId" : LUUID("27f2b4a6-5471-406a-a39b-1e0b0f8c4eb9"),
  47.     "reference" : {
  48.         "type" : "someType",
  49.         "id" : LUUID("331503FB-C4D1-4F7A-A461-933C701EF9AB")
  50.     },
  51.     "rootReferenceId" : LUUID("27f2b4a6-5471-406a-a39b-1e0b0f8c4eb9"),
  52.     "rootReferenceIdAsString" : "27f2b4a6-5471-406a-a39b-1e0b0f8c4eb9",
  53.     "displayName" : "Child 1 (Root 2)"
  54. }
  55.    
  56. /* Child 2 - Root 1 */
  57. {
  58.     "_id" : LUUID("6dd8c8ed-4a60-41ca-abf1-a4d795a0c213"),
  59.     "parentId" : LUUID("9f3a73df-bca7-48b7-b111-285359e50a02"),
  60.     "reference" : {
  61.         "type" : "someType",
  62.         "id" : LUUID("23E8B540-3EFB-455A-AA5C-2B67D6B59943")
  63.     },
  64.     "rootReferenceId" : LUUID("9f3a73df-bca7-48b7-b111-285359e50a02"),
  65.     "displayName" : "Child 2 (Root 1)"
  66. }
  67.    
  68. db.getCollection('test').aggregate([
  69.     {
  70.         $match: {
  71.             rootReferenceId: { $ne: null }
  72.         }
  73.     },
  74.     {
  75.         $group: {
  76.             _id: "$rootReferenceId",
  77.             referenceIds: { $addToSet: "$reference.id" }
  78.         }
  79.     }
  80. ])
  81.    
  82. /* 1 */
  83. {
  84.     "_id" : LUUID("27f2b4a6-5471-406a-a39b-1e0b0f8c4eb9"),
  85.     "referenceIds" : [
  86.         LUUID("331503fb-c4d1-4f7a-a461-933c701ef9ab")
  87.     ]
  88. }
  89.  
  90. /* 2 */
  91. {
  92.     "_id" : LUUID("9f3a73df-bca7-48b7-b111-285359e50a02"),
  93.     "referenceIds" : [
  94.         LUUID("23e8b540-3efb-455a-aa5c-2b67d6b59943"),
  95.         LUUID("331503fb-c4d1-4f7a-a461-933c701ef9ab")
  96.     ]
  97. }
  98.    
  99. {
  100.     LUUID("27f2b4a6-5471-406a-a39b-1e0b0f8c4eb9") : [
  101.         LUUID("331503fb-c4d1-4f7a-a461-933c701ef9ab")
  102.     ]
  103.     LUUID("9f3a73df-bca7-48b7-b111-285359e50a02") : [
  104.         LUUID("23e8b540-3efb-455a-aa5c-2b67d6b59943"),
  105.         LUUID("331503fb-c4d1-4f7a-a461-933c701ef9ab")
  106.     ]
  107. }
  108.    
  109. db.getCollection('test').aggregate([
  110.   { "$match": { "rootReferenceId": { "$ne": null }}},
  111.   { "$group": {
  112.     "_id": "$rootReferenceIdAsString",
  113.     "referenceIds": { "$addToSet": "$reference.id" }
  114.   }},
  115.   { "$group": {
  116.     "_id": null,
  117.     "data": {
  118.       "$push": { "k": "$_id", "v": "$referenceIds" }
  119.     }
  120.   }},
  121.   { "$replaceRoot": { "newRoot": { "$arrayToObject": "$data" }}},
  122.   {
  123.     "$project": {
  124.       "difference": { "$setDifference": [ "$9f3a73df-bca7-48b7-b111-285359e50a02", "$27f2b4a6-5471-406a-a39b-1e0b0f8c4eb9" ] }
  125.     }
  126.   }
  127. ])
  128.    
  129. db.getCollection('test').aggregate([
  130.   { "$match": { "rootReferenceId": { "$ne": null }}},
  131.   { "$group": {
  132.     "_id": "$rootReferenceId",
  133.     "referenceIds": { "$addToSet": "$reference.id" }
  134.   }},
  135.   { "$group": {
  136.     "_id": null,
  137.     "data": {
  138.       "$push": { "k": { "$toString": "$_id" }, "v": "$referenceIds" }
  139.     }
  140.   }},
  141.   { "$replaceRoot": { "newRoot": { "$arrayToObject": "$data" }}}
  142. ])
  143.    
  144. db.getCollection('test').aggregate([
  145.   { "$facet": {
  146.     "allInRoot1": [{
  147.       "$match": { "rootReferenceId": LUUID("9f3a73df-bca7-48b7-b111-285359e50a02") }
  148.     }],
  149.     "allInRoot2": [{
  150.       "$match": { "rootReferenceId": LUUID("27f2b4a6-5471-406a-a39b-1e0b0f8c4eb9") }
  151.     }]
  152.   }}, {
  153.     "$project": {
  154.       "difference": {
  155.         "$filter": {
  156.             "input": "$allInRoot1",
  157.             "as": "this",
  158.             "cond": { "$in": [ "$$this.reference.id", { "$setDifference": [ "$allInRoot1.reference.id", "$allInRoot2.reference.id" ] } ] }
  159.         }
  160.       }
  161.     }
  162.   }
  163. ])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top