Schema design in a document database

We are using MongoDB on our project, since mongo is document store, schema design is somewhat different, when you are using traditional RDBMS data stores, one thinks about tables and rows, while using a document database you have to think about the schema in a some what different way. Lets say, we want to save a customer object, when using a RDBMS we would come up with Customer, Address, Phone, Email. They are related to each other as shown below. {% img /static/images/customer.jpg 448 367 Relational design %} When doing a document database, the schema design actually does not change much, the Customer document contains an array of Addresses, a one to many relationship. You will not need the FK columns or the Primary Key columns on the child tables, since the child rows are embedded in the parent object. The JSON object below shows how the data would look.

``` json Document representing Customer { “_id” : ObjectId(“4bd8ae97c47016442af4a580”), “customerid” : 99999, “name” : “Foo Sushi Inc”, “type” : “Good”, “since” : “12/12/2001”, “addresses” : [{ “address” : “4821 Big Street”, “city” : “Stone”,
“state” : “IL”, “country” : “USA” }, { “address” : “1248 Barlow Ln”, “city” : “Hedgestone”,
“country” : “UK” }
], “emails” : [ {“email” : “”}, {“email” : “”} ], “phones” : [ {“phone” : “773-7777-7777”}, {“phone” : “020-6666-6666”} ] }

So Instead of 1 Row for customer, 2 rows for address, phone and email each, you get one Customer document. If you want to query for customers in USA. Using RDBMS you would do

``` sql Customer search using SQL
SELECT FROM customer, address
WHERE customer.customerid = address.customerid

The same query in mongo would look like

sql Customer search using mongo client db.customers.find({"":"USA"},{"name":true})

where customers is the collection in which we store the customers information.