design - Collection data modeling in mongoDB -
i want design model profiles interaction, example <-> interact <-> b , interaction contains common fields , b.
lets have collection called interactions, have few thoughts in mind , looking best practice solution.
separate interaction 2 different documents, 1 each profile
{ pid:"a id" commonfield1:"" commonfield2:"" .. } { pid:"b id" commonfield1:"" commonfield2:"" .. }
pros: fast read
cons: each update common field should performed on both of documentsmaintain 1 document interaction
{ pids:['a id','b id'] commonfield1:"" commonfield2:"" .. }
pros: update common field once
cons: tricky read
the thing there lot of reading lot of updating , collection should designed lot of millions of documents.
common queries in scenarios:
- retrieve profile interactions
- update specific profile interaction
i leaning second choice relying on multikey index on pids fast document lookup , enjoying in single update in each frequent change.
i have no experience in sharded collections have noticed multikey index not supported sharding key, should show stopper second choice?
does reads fast enough kind of index? , other choices use case?
your answer highly appreciated.
i think latter format makes more sense avoid duplication of updates.
for interaction pairs should using compound index rather array. compound index can be used _id
, shard key (arrays not valid either).
so document might like:
{ _id: { pid1: 'a', pid2: 'b' }, commonfield1: '', commonfield2: '', }
if want avoid duplicate pairs sort ids in predictable order. example, pid1
might lesser of 2 values.
the default _id
index allow efficiently either (pid1,pid2) or (pid1) interactions, you'll want add index on {'_id.pid2': 1}
.
Comments
Post a Comment