I’m currently putting together a data structure for a new application I’m putting together and a really basic question I have is regarding grouping data within a document. By this I mean putting together a structure like this:
_id
Question.Id
Question.Name
Rather than
_id
QuestionId
QuestionName
When I’m looking at the data in my collection it would certatinly be simpler if I can group the data together as in the first example.
Are there any downsides to this? Does it slow down performance? Is there any documentation I can be pointed to that will help me make an informed choice?
I’ve had a look at MongoDb documentation on sub-document querying and can’t find anything that says you shouldn’t do it or that it has negative impacts on performance, sorry I’ve phrased my original message so poorly.
Hi @Chris_Boot1 and welcome to MongoDB community forums!!
A general rule of thumb while doing schema design in MongoDB is that you should design your database in a way that the most common queries can be satisfied by querying a single collection, even when this means that you will have some redundancy in your database. Thus, it may be beneficial to work from the required queries first, making it as simple as possible, and let the schema design follow the query pattern. You can read more about Data Modelling and Query Performance to understand further.
From the above two schema shared, the former displaying use of Embedded Documents and the later a plain document which contains all data into a single field.