Question

First of all, i'm newbie for DB design & MongoDB.

I've a DB structure like this,

title
description
[generic_object]
date_created
date_updated
...

generic_object refers to different content types, like these;

food_venue: {lat: long:}

OR

drink_size : 1
drink_brand : "guiness"
drink_type : 3

i'm trying to tell generic objects has different types/jobs.

i've an idea like these, but i dont think its the best way

title
description
type : food or drink
food : {food_id: ... $ref: food model}
drink : {drink_id: ... $ref: drink model}
...

if i add a drink object, food object going to be null or blank.

What is the most best performance way to design this DB in MongoDB (or MongooseJS)?

Was it helpful?

Solution

MongoDB is a "schemaless" database, so your stored documents can be different. In your generic object you can simply save any subdocument you want (food or drink). Anyway, for convenience, you should have a structure in your database, so you can add some extra fields. In your case:

{
  title:"title",
  description:"description",
  generic:
  {
    type:"food",
    field1:"field1",
    field2:"field2",
    field4:"field3",
  }
}

{
  title:"title2",
  description:"description2",
  generic:
  {
    type:"drink",
    otherField1:"field1",
    otherField2:"field2",
  }
}

MongoDB has a limit of 16MB per document, so if you are pretty sure that you will not use that amount of space, you can embed all information on one document.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top