Over a million developers have joined DZone.

Sample Apps: Spring Data MongoDB and JSF Integration Tutorial (Part 2)

· Database Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database. Try it now! Brought to you in partnership with MongoDB.

MongoDB schema design and data preparation


MongoDB Introduction

MongoDB is a open-source scalable, high-performance NoSQL database. It is a document-oriented Storage. It can store JSON-style documents with dynamic schemas. In this application, each product is stored as JSON-style document in MongoDB.


Schema Design in MongoDB

Each product in the catalog contains general product information (e.g. sku, title, and product type), price details (e.g. retail and list price) and product sub-details (e.g. tracks of audio CDs / chapters of books).


In this application, MongoDB is used. The schema design will be focus more on the data usage. It is different from traditional RDBMS schema design. The schema design in MongoDB should be:


Sample Data:

x= {
  sku: "1000001",
  type: "Audio Album",
  title: "A Love Supreme",
  description: "by John Coltrane",
  publisher: "Sony Music",
  pricing: {
    list: 1200,
    retail: 1100
  },
 
  details: {
    title: "A Love Supreme [Original Recording Reissued]",
    artist: "John Coltrane",
    genre:  "Jazz" ,
    tracks: [
      "A Love Supreme Part I: Acknowledgement",
      "A Love Supreme Part II - Resolution",
      "A Love Supreme, Part III: Pursuance",
      "A Love Supreme, Part IV-Psalm"
    ],
  }
}
 
y= {
  sku: "1000002",
  type: "Audio Album",
  title: "Love Song",
  description: "by Khali Fong",
  publisher: "Sony Music",
  pricing: {
    list: 1000,
    retail: 1200
  },
 
  details: {
    title: "Long Song [Original Recording Reissued]",
    artist: "Khali Fong",
    genre:  "R&B",
    tracks: [
      "Love Song",
      "Spring Wind Blow",
      "Red Bean",
      "SingAlongSong"
    ],
  }
}
 
 
z= {
  sku: "1000003",
  type: "Book",
  title: "Node.js for PHP Developers",
  description: "by Owen Peter",
  publisher: "OReilly Media",
 
  pricing: {
    list: 2500,
    retail: 2100
  },
 
  details: {
    title: "Node.js for PHP Developers",
    author: "Mark Owen",
    genre:  "Technology",
    chapters: [
      "Introduction to Node",
      "Server-side JS",
      "PHP API",
      "Example"
    ],
  }
}

Sample query to add the data:

db.product.save(x);
db.product.save(y);
db.product.save(z);

Sample query to test the sample data:

db.product.find({'sku':'1000004'});
db.product.find({'type':'Audio Album'});
db.product.find({'type':'Audio Album', 'details.genre': 'Jazz'});


Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.

Topics:

Published at DZone with permission of Boris Lam, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}