Home > Software engineering >  Mongo DB how to find orphans in a tree?
Mongo DB how to find orphans in a tree?

Time:05-19

I have the trees collection with objects which have a tree like structure. I'm using child references pattern actually, but in my case the depth of each tree is undefined - it could be in range [0:100] approximately:

{
  "_id": "tree1",
  "tag_id": "1",
  "children": [
    {
      "_id": "tree2",
      "tag_id": "2",
      "children": [
        {
          "_id": "tree3",
          "tag_id": "3"
        }
      ]
    },
    {
      "_id": "tree4",
      "tag_id": "4"
    }
  ]
}

also I have the simple tags collection with objects like:

[
  {
    "_id": "1",
    "name": "one"
  },
  {
    "_id": "2",
    "name": "two"
  },
  {
    "_id": "3",
    "name": "three"
  },
  {
    "_id": "4",
    "name": "four"
  },
  {
    "_id": "5",
    "name": "five"
  }
]

As you can see, I have the orphan (5) which doesn't have a link inside trees collection:

{
    "_id": "5",
    "name": "five"
}

I want to find all such orphans and remove them from tags collection. I've tried to use aggregate method with $lookup:

db.tags.aggregate(
 [
  {$lookup:
    {
        from: "trees",
        localField: "_id",
        foreignField: "tag_id",
        as: "matched_docs"
    }
 }
 ]
);

But such approach joins and finds only "top" connections without looking to children array. How can I solve the task using mongo queries? Maybe, it's better to use different data-structure, pattern or other aggregation method?

CodePudding user response:

As per the documentation of latest version

Nested Depth for BSON Documents

MongoDB supports no more than 100 levels of nesting for BSON   
documents. Each object or array adds a level.

Mongodb is not fit for this schema. Hence, you cannot achieve this with huge dynamic levels of deeply nested arrays.

You can alter the schema or data store.

  • Related