[Solved] Update/Insert mulitple documents with relations


#1

Hey,

lets say i have a Persons collection and a child which is stored in the collection. I want to create two parents as documents and store their relationships. There are two kinds of relationships.

  1. Relationship between parents (married, divorced, …) and 2. the relationship to their child (adopted, biological). So I have to create both parents and store the other parents _id in the other parent.
parent1: {
  ...
  partners: {
    [{
      _id: parent2._id,
      type: relationshipBetweenParents
    }]
 },
 children: {
   [{
     _id: child._id,
     with: parent2._id,
     type: relationshipWithChild
   }]
 }
}

and the same for parent2. My problem is ‘knowing’ the _id of parent2 before I created that person and storing it in person1.

Any suggestions on how to solve this?


#2

Pretty please? :smiley:

(Post must be at least 20 characters)


#3

I have similar functionality in the app I’m building and have relationships nested within the person document. In my app the children are the primary record that is added first so I have their id. Then throughout my processes as parents are added, I have a server method that creates the parental reciprocal relationships to the child.

For the parent->parent relationships, you can ask the marital status on each parent or after parent2 is added and then use a meteor method after parent2 is created to create the reciprocal relationships.


#4

My idea was to use the callback function of the inserts and updates to do the next step. Is that what you are doing?


#5

Exactly, I’m passing the ID created or updated to a method to create reciprocal relationships.