0

I'm trying to use mongoose to represent an association between 3 schemas:

Schema1: {
    //fields
}

Schema2: {
    //fields    
}

Scehma3: {
   //fields
   - [Scehma1:Scehma2] - collection of key-val elements
     where the key is a ref to schema1 and val is ref to scehma2
}

Does mongoose support this king of association without creating a schema4?

2

1 Answer 1

1

You can't create ambiguous keys in mongoose because its whole purpose is to handle your document structure for you. What you can do, however, is create an array of objects.

Schema4: {
  schemaRefs: [{
    refToSchema1: {type: mongoose.Types.ObjectId, ref: 'Schema1'},
    refToSchema2: {type: mongoose.Types.ObjectId, ref: 'Schema2'}
  }]
}

For future reference it's far easier to understand your question when you provide real examples rather than false names. Even if you falsify your example (e.g. some relationship between restaurants and customers or something) it's much easier to understand the relationship you're trying to make.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey! looks like a good direction for me, following you suggestion I can use "schemaRefs in schema3 no? why do I need a new schema for that
oops you don't i just miscounted :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.