0

Hi I am newbie to mongodb.I am using java.

I have 2 tables system, system_properties in my relational table.

Something like this.

       Table              Fields                                        values

      System           System_ID(PK), System_Info                  100, cisc
                                                                   200, Gets
      System_prop      System_ID(FK), Prop_key, Description        100, good, success
                                                                   100, better,progress  
                                                                   200, worse,failed

I am trying to create a schema for this. eg: for inserting one doc

      System
         {
             "_id" : 100
             "System_Info" : "cisc"
             System_Properties :
                 { "system_id":100
                        [{prop_Key : "good", Description: "success"}, 
                         {prop_Key : "better", Description: "progress"}] }
         }

Is this the best schema for this design?

To avoid joins only we are embedding documents.

I have a doubt whether system_id : 100 (Foreign key in relational tables) is necessary since "_id" = 100 (primarykey in Relational table)is also refers the same.

Ramya
  • 1,067
  • 6
  • 16
  • 29

3 Answers3

1

Foreight key in your properties object is not necessary as you have embedded them into document. System properties shall be just an array, or even object like:

{
     "good": "success",
     "better": "progress"
}
Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
1

There are several ways to design that schema, even using two collections. So:

How does your app use the stored information?

Do you have an unbounded growth in that embedded properties? => bad idea!

Are there some with just two properties, some with 100, and some with 1k? => try to stick to one size of docs in a collection. (use bucketing and if you want favs/best 5 for your main document)

more Info: Talk about Schemadesign by Eliot Horowitz

Marc
  • 461
  • 2
  • 5
0

Schema design is very important to the efficiency of your database model. You want to avoid trying to think about your schema design for mongodb in a relational database mindset. Specifically to your question, you do not need to have a foreign key for system id, since you are embedding System Properties into your System document. Also, your System Properties fields can just go directly into your System document without being embedded, as Konstantin as pointed out above.

Here's some links to get you started thinking about what considerations are important to make when doing schema design in mongodb:

Louisa
  • 861
  • 6
  • 5
  • 1
    http://www.manning.com/banker/ (MongoDB in Action) http://www.10gen.com/presentations/mongosf2011/schemabasics http://www.10gen.com/presentations/mongosv-2011/schema-design-by-example http://www.10gen.com/presentations/mongosf2011/schemascale And here are some sample schema designs: http://docs.mongodb.org/manual/use-cases/ – Louisa Sep 05 '12 at 15:33