i am writing an application using mongodb in which user can send messages to each other here are the fields i want to store
user_to
user_from
message
sent
ip
unread
what i am asking is if user A sends msg to user B for eg:
user_to : B,
user_from : A,
message :hello world,
sent:1334901545,
ip : XX.XX.XX.X,
unred : true
this is the format how data is stored for first time but when again user A sends message to user B how should i store that data should ??
i have thought of two ways please tell me which is most efficient
user_to : B,
user_from : A,
message :hello world again,
sent:1334901745,
ip : XX.XX.XX.X,
unred : true
or
user_to : B,
user_from : A,
messages : array(
array(message="hello world",sent="1334901545",ip: XXX.X.XX.XX,unread:true),
array(message="hello world again",sent="1334901745",ip:XX.XX.XX.X,unread:true )
)
first type is very simple in structure better for querying and very easy to analyse but i think it increase duplicate data and will consume larger disk space as application grows
whereas second type eliminates duplicate record and low on disk space but it complicates data retrieval difficult to read cannot be used efficiently to write amd many other complication
i just want to know which is the right way considering mongodb is highly scaleable should i go for simplicity or eliminate duplicate records and save disk space
Separate records will be better for query speed, I would go with that. Also in your example there is not so much redundant data. If there was you could always factor it out into a new collection.