Sunday, 15 August 2010

node.js - Structuring one-to-one interdocument relationship MongoDB -



node.js - Structuring one-to-one interdocument relationship MongoDB -

i'm starting out using mongodb , i'm bit lost @ how construction documents tackle next problem (note basic illustration matches i'm having difficulty with).

the problem:

let's have top-level document called family, these documents stored in collection called families , contain basic info family, i.e.

{ _id: objectid("foobar"), familyname: "simpson", address: "743 evergreen terrace, springfield", children: [ { _id: objectid("barfoo"), firstname: "bartholomew", preferredname: "bart", middlename: "jojo" } // ... etc. ] }

now, let's i'm adding top-level document application: school, stored in collection called schools. need relate each kid school, , each kid may attend 1 school @ point in time. how approach in mongo? i've come heavy rdbms background , i'm having bit of difficulty figuring out. main issues i've come against in solutioning revolve around fact i'll need efficiently handle next use-cases:

view school , able see children enrolled there view family , see of schools children enrolled in what i've tried: storing kid references in `school`

the first solution went create enrollments array in school document referenced _id of kid total name convenience, i.e.

{ _id: objectid("asdadssa"), name: "springfield elementary", enrollments: [ { child_id: objectid("barfoo"), // bart simpson fullname: "bart simpson" // concatenation of preferredname , familyname } ] }

this seemed fantastic first use-case, needed display of students enrolled @ particular school.

however when turned sec use-case realised may have made mistake. how on earth figure out school each kid in family belonged to? way see traverse every single school in schools collection, drill downwards enrollments , see if child_id matched kid in family...doesn't seem efficient it? led next attempt.

storing reference school in kid object

because each kid can belong 1 school figured maybe store reference school document in each kid sub-doc, i.e. bart's document become:

{ _id: objectid("barfoo"), firstname: "bartholomew", preferredname: "bart", middlename: "jojo", school_id: objectid("asdadssa") }

now second use-case happy, first unsatisfied.

conclusion

the way can see both use-cases beingness satisfied if employ both solutions simultaneously, i.e. store school_id in kid sub-doc , store child_id in enrollments array.

this seems clunky me, means you'll need @ to the lowest degree 2 writes per enrollment alter (to remove school , alter child). far i'm aware mongodb has atomic writes , no transaction back upwards looks place info integrity potentially suffer.

if mongodb gurus propose alternate solution that'd great. i'm aware particular problem screams "rdbms!!!!", little part of application , of other info lends document store.

i'm in planning stage i'm not 100% committed mongo, thought i'd give crack since i've been hearing things it.

for little utilize case you've described, switch families collection people or students collection

{ "_id" : objectid("barfoo"), "family_id" : objectid("spamneggs"), "name" : { "first" : "bartholomew", "last" : "simpson" }, "school_id" : objectid("asdadssa") }

that stores students separate documents unites them mutual family_id (i snuck in way store names). schools can have school info without enrollments. i'll give illustration code in mongo shell 2 utilize cases. find children enrolled in bart's school , bart's school's document:

> db.students.find({ "school_id" : objectid("asdadssa") }) > db.schools.find({ "_id" : objectid("asdadssa") })

to find of schools bart's family has children enrolled in:

> var schools = [] > db.students.find({ "family_id" : objectid("spamneggs") }, { "_id" : 0, "school_id" : 1 }).foreach(function(doc) { schools.push(doc.school_id) }) > db.schools.find({ "_id" : { "$in" : schools } })

both of these simple application-side joins , should work fine in case because 1 family won't have zillions of children. indexes on school_id , family_id help.

for writes, pupil document needs updated proper school_id.

node.js mongodb database-design schema

No comments:

Post a Comment