In MongoDB and Java, I am building a binary tree using closure table concept. Say my initial data is as below:
{
"userid" : "1", "parent" : "root", "position" : "",
"ancestry": ["1"]
}
# ...
{
"userid" : "7", "parent" : "5", "position" : "right",
"ancestry": ["1", "5", "7"]
}
{
"userid" : "10", "parent" : "7", "position" : "right",
"ancestry": []
}
When inserting new user, I want to set its ancestry by: - get parent ancestry - insert to child ancestry - then add child userid (this is no issue using addToSet after)
For user 3, result will be:
"ancestry": ["1", "5", "7", "10"]
How do I elegantly get the array from the parent, copy it to the child? I am using MongoDB Java Driver 3.x, kindly help.
Thank you before.