mongoose - Mongodb database schema design issue -


i have 2 collections in mongodb database :- users , clubs

user schema :-  var userschema = new schema({ name: {     type: string ,     required: true }, clubs: [     {type: mongoose.schema.types.objectid, ref: 'club'} ]}); 

now when user joins club , update club array . need fetch users particular club . therefore creating club schema :-

var clubschema = new schema({ clubname : {     type: string ,      unique: true ,     required: true }, members : [ {type: mongoose.schema.types.objectid,      ref: 'user' ,      default: [] } ]}); 

my question : right way , or should maintain club information @ user collection ? maybe need optimize query related fetching users belonging club.

it's quite hard what's "the right way" honest, case case depending on application queries , architecture.

i have seen people designed above; solving many-to-many relationships using reference in both collection. work case queries above:

db.user.find({"clubs": objectid("000000")}); #find users belonging club. db.club.find({"users": objectid("111111")}); # find clubs user belong to.

with 2 indexes on:

db.user.ensureindex({"clubs": 1});

db.club.ensureindex({"users": 1});

though may reduce overall consistency. i.e. when delete club, need update other affected documents well. while in process of mentioned update, documents may not up-to-date. if don't have mongodb on replicas, not accessing db distributed systems , think problem not big issue, go design ease of query.

if think consistency mentioned above deal-breaker, go inserting clubs reference in users collection. common design, , 1 listed in mongodb official site.

http://docs.mongodb.org/manual/tutorial/model-referenced-one-to-many-relationships-between-documents/

i suggest optimising query first, before choosing add complexities in update/insert (because have update related docs having ref in users , clubs).

hope helps.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -