Hello,
I’m trying to design “has many” and “many to many” relationships without using view to avoid indexing delay and consistency issue.
After reading a few articles, I’ve come up with the following practice. Let’s assume we have Student and Subject.
Student.1
Subject.1
Subject.2
Subject.3
When I want to have Student.1 has many Subjects relationship, I should have
Student.1
Student.1.subjects.counter << Couchbase counter
Student.1.subjects.1 << the id doesn’t represent id of the subject. It’s from the Student.subjects.counter
{
"ref": "Subject.1"
}
Student.1.subjects.2
{
"ref": "Subject.2"
}
to query subjects of Student.1, I need to issue four queries.
- get Student.1
- get Student1.subjects.counter
- loop through the number from #2 and create an array of “Student.1.subjects.:number_here”
- now, use multiGet with ids from #3.
- make an array of “ref” value and make another multiGet
pseudo code for #3
for($i=1; $i<=Student1.subjects.counter; $i++) {
$subjectsIds[] = “Student.1.subjects.”.$i;
}
Are there any better solutions?