L
L
lopatin_ay2015-06-15 18:12:51
MongoDB
lopatin_ay, 2015-06-15 18:12:51

How to properly link parent and child in mongodb?

I have a fairly large tree, after reading the documentation, a solution with a parent and a child is more suitable for me. The documentation lists the following examples:

db.categories.insert( { _id: "Books", parent: null } )

db.categories.insert( { _id: "Programming", parent: "Books" } )

In the database, we get 2 separate documents.
First
{
    "_id": "Books",
    "parent": null
}

Second
{
    "_id": "Programming",
    "parent": "Books"
}

For some reason, this moment confuses me, and it seems to me that the document should be 1, and it is already branching, something like this
{
  "_id": "Books",
  "parent": null
   children: [
    { "_id": "Programming", 
      "parent": "Books" 
      children: [
        ....
    },
    ....
  ]
}

Tell me, please, how should it be? I can’t find examples, and how can I then bypass this whole tree?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question