CS代写 INFO 30005

INFO 30005
Web Information Technologies

This lecture

Copyright By PowCoder代写 加微信 powcoder

• relational vs NoSQL databases
• MongoDB: a document database
• data modelling with MongoDB
• doing CRUD with MongoDB
• MongoDB tools: Atlas, Robo, Compass
• on to Mongoose (next lecture)

Relational vs NoSQL

Recap: relational database
• all data are stored in TABLES
• tables are NORMALIZED and a SCHEMA is defined
• primary and foreign KEYS
• tables can be JOINED in SQL queries
• difficult to distribute data across multiple servers

Some data is well-suited to tables

But some data is not table-shaped
Sometimes one aggregate must be stored across many tables
Apps and developers work hard to dissemble and reassemble the aggregate
For detail see Thoughtworks article at https://www.thoughtworks.com/insights/blog/n osql-databases-overview

Relational Schemas
Designer defines a schema
Now all data must strictly conform to the schema
Schema says what data is allowed

MongoDB objects

MongoDB vs relational
• collections instead of tables
• JSON documents instead of rows
• no joins (can link or embed)
• no schema-related error checking
• developer-friendly (schema can evolve)

Data Modelling

case: Food Buddy app
Can be used without logging in
• Browse foods: scroll through hundreds of foods, or type into the search bar
• See details of one food: name, photo, description, nutritional data (calories, fat, protein, carbs, vitamins A to K, sodium, calcium, iron per serving as % of daily recommendation)
• Set a food filter: click buttons to prefer or avoid particular kinds of foods
• Set goals: more/fewer calories, sugar, protein, fat,
Require login
• Enter profile details (name, gender, birth year, email, password)
• Set food filter permanently
• Set favourites: when looking at a detail page, click ‘heart’ button
• Track foods eaten: when looking at a detail page, click ‘I ate it” button
• Show lists, stats and charts based on ‘I ate it’ data over time

Relational
Should we store this data in 1, 2, 3 or 4 collections? Which collections will the app query?
We will definitely query Foods and Users
Favourites? Eaten? Probably only via their particular User. So they can be embedded in User.
Data Modelling

Data Modelling
_id: ObjectId, email: string, password: string, nameFamily: string, nameGiven: string, gender: string, yearBorn: number, favourites: [
{foodId: ObjectId} ],
{foodId: ObjectId
when: date} ]
Relational
_id: ObjectId, name: string, photo: string, description: string, vegan: boolean, organic: boolean, calories: number, fat: number, protein: number

Data Modelling
_id: ObjectId, email: string, password: string, nameFamily: string, nameGiven: string, gender: string, yearBorn: number, favourites: [
{foodId: ObjectId} ],
{foodId: ObjectId
when: date} ]
_id: ObjectId, name: string, photo: string, description: string, vegan: boolean, organic: boolean, calories: number, fat: number, protein: number

Data Modelling
_id: ObjectId, email: string, password: string, nameFamily: string, nameGiven: string, gender: string, yearBorn: number, favourites: [
{foodId: ObjectId} ],
{foodId: ObjectId
when: date} ]
_id: ObjectId, name: string, photo: string, description: string, vegan: boolean, organic: boolean, calories: number, fat: number, protein: number

Using MongoDB

MongoDB tools

CRUD (with SQL equivalents)
• db.collection.insertOne( )
• db.collection.update( , , { upsert: true } )
• db.collection.find( , )
• db.collection.findOne( , )
• db.collection.updateOne( , , )
• db.collection.updateMany( , , ) • db.collection.replaceOne( , , )
• db.collection.deleteOne( , )
• db.collection.deleteMany( , ) 18
“insert” “select”
“update” “delete”

Example MongoDB queries
// show a list of all databases
// work on this database
// show all collections in this database
use FoodBuddy
show collections
db.foods.find( {}, {} )
db.foods.find( {name: “Apple”}, {} ) // similar to “select * where name = …” db.foods.findOne( {name: “Apple”}, {name: true, description: true } ) db.foods.updateOne( {name: “Apple”}, {$set: {description: “Apples are cool” }}) db.foods.insertOne( {name:”chair”, type:”furniture”} ) // whoops! db.foods.deleteOne( {name:”chair”} )
// similar to “select * from …”

Using MongoDB in Node.js
db.collection(‘foods’).find()
db.collection(‘users’).findOne ( ObjectId(req.params.id) )
db.collection(‘foods’).findOne ( ObjectId(req.params.id) )

next lecture …
validates and translates objects between app and MongoDB

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com