Home > Software engineering >  Best way to structure data in MongoDB - Multiple documents or a document with an array
Best way to structure data in MongoDB - Multiple documents or a document with an array

Time:10-25

I want to get data from a temperature and humidity sensor i have at home and ingest it in MongoDB. It will ingest the data every hour

What is the best approach, create a document for every hourly reading, or create a document with an array, and add each new reading as a new item to the array, keeping all the data in a single document?

Thanks

CodePudding user response:

It depends on what you are gonna do with the data, but I would not create a single document with an array because it would be impossible to index.

What I would do is store each reading as a new document, this way you can index both time and temperature/humidity depending on what you wanna do.

a simple approach would look something like this

{
    timestamp: int64,
    temperature: float32,
    humidity: float32
}

With that said considering that you only need to keep the reading of two sensors taken every hour, any approach will work fine: any kind operation you do will likely be instantaneous , you don't really need indexes and you don't need to consider performance or space. You might not even need a database, a csv file would be enough for this use case.

CodePudding user response:

I would recommend you to read this guide from MongoDB itself

https://www.mongodb.com/developer/article/mongodb-schema-design-best-practices/

  • Related