Coder Social home page Coder Social logo

mongo-structured-data's Introduction

Structured Data in Mongo

Why is this important?

This workshop is important because:

Organizing stored data in a predictable way essential to making it useful and actionable when an application performs searches and queries to derive information for its users.

What are the objectives?

After this workshop, developers will be able to:

  • Gain a deeper understanding of Mongo's ability to handle data organization
  • Compare and contrast embedded & referenced data
  • Design routes for nested resources
  • Build the appropriate queries for nested relationships

Where should we be now?

Before this workshop, developers should already be able to:

  • CRUD data in Mongo
  • Use Mongoose to write a Schema

Data Organization in Mongo

There are two ways to form relationships in a document-based database...

Embedded Data

  • Embedded Data is a document directly nested inside of other data

Uniquely identifying information, address information, user-generated content.

In Mongo directly, we treate embedded data as a field that is itself an object. We can use JavaScript notation to define the object directly.

Referenced Data

  • Referenced Data contains an id of a document that can be found somewhere else

Memberships, relationships, shared content

In this scenario, we store the data we want in a new collection, then reference it. For example, if we want our Account documents to reference our Orders documents, we need to create two collections - Accounts and Orders. We create an order and then add the order id to a field in the account document. If we want to see the order information associated to an account, we first need to search for our account and then once we have the account we can search for the order information.

There is generally a tradeoff between efficiency (embedded) and consistency (referenced) depending on which one you choose.

Scenario:

For each situation would you use embedded or referenced data? Discuss with a partner and be prepared to explain why.

  • A User that has many Tweets?
  • A Food that has many Ingredients?

Implementation with Mongoose

Tip: Noteworthy code lines are denoted with the comment: NOTE.

Referencing Data

var foodSchema = new Schema({
  name: {
    type: String,
    default: ""
  },
  ingredients: [{
    type: Schema.Types.ObjectId,  // NOTE: Referencing
    ref: 'Ingredient'
  }]
});

var ingredientSchema = new Schema({
  title: {
    type: String,
    default: ""
  },
  origin: {
    type: String,
    default: ""
  }
});

Embedding Data

var tweetSchema = new Schema({
  body: {
    type: String,
    default: ""
  }
});

var userSchema = new Schema({
  username: {
    type: String,
    default: ""
  },
  tweets: [tweetSchema]	  // NOTE: Embedding
});

Queries Exercise

Goal

Create and navigate through relational data in MongoDB

Setup

  • startup mongoDB with mongod
  • cd into the folder starter-code in this directory
  • npm install to bring down all the dependencies
  • node console.js to enter into a REPL where you can interact with your DB

Tips

  • save your successful code into your text-editor for each successful step
  • <command> + <up> will bring you to the last thing you entered in the repl

Note: All your models will be nested inside an object db.

Steps

1) Create a user

2) Create tweets embedded in that user

3) List all the users

4) List all tweets of a specific user

5) Create several ingredients

6) Create a food that references those ingredients

7) List all the Foods

8) List all the ingredients in a Food

Licensing

All content is licensed under a CC­BY­NC­SA 4.0 license. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].

mongo-structured-data's People

Contributors

zebgirouard avatar nickandersonr avatar ilias-t avatar jpbarela avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.