#Mongoose Schema notes
-
In mongoose, models correspont to a collection of records in mongo.
-
handling relational data with mongo
- single to many: if a child object can ony have one parent, and a parent model can have many children objects it is best to use an array of children as a nested property inside the parent model (in mongo this is reffered to as sub-documents).
for example:
- a user has many blog posts, but a post is owned by only one user. Therefore, a good model schema would be an embedded collection of posts inside a user model:
const PostSchema = new Schema({ title: String, body: String }); const UserSchema = new Schema({ name: String, postCount: Number, posts: [PostSchema] // an array of posts });
- single to many: if a child object can ony have one parent, and a parent model can have many children objects it is best to use an array of children as a nested property inside the parent model (in mongo this is reffered to as sub-documents).
for example:
-
virtual types: virtual type are properties in the db that are directly linked to other properties in the database (e.g. the length of an array). Instead of saving both properties in the database and having to keep track of both, we can save a virtual type that will be computed in the server evvery time a document is pulled from the databse. for example:
- we want to keep track of a user's post count. And since we keep an array of posts for each user, we can define postCount as a virtual type.
- Create the DB schema:
const UserSchema = new Schema({ name: String, posts: [PostSchema] });
- create the virtual post:
UserSchema.virtual('postCount').get(function() { return this.posts.length; });
-
Indexes: By default, every collection in mongo has an index on the _id property. Having an index on a property allows to find records very fast (in O(1)). In order to enable text search for a specific property, there must be an index on that property. Mongo allows to add another index to a specific property. How to add custom indexes:
- Start mongo shell and use the desired DB
mongo use upstar_music
- create the index for the desired collection
db.artists.createIndex({ name: "text" })
###Node\Express Application Basics Node is an engine for running JS outside of a browser environment.
Express is a framework to simplify working with HTTP requests.
####Creating an application:
- Create app:
npm init 'appname'
- install dependencies:
npm install --save mongoose express mocha
- Generate a new express app:
-
create app.js and index.js in the main directory:
app.js:
const express = require('express'); const app = express(); module.exports = app;
index.js:
const app = require('./app'); app.listen(3050, () => { console.log('Running on port 3050') });
-
Define request route handlers: inside app.js add the following:
const express = require('express'); const app = express(); // Defing route handlers for HTTP requests: // Watch for incoming request of method GET to the routte http://localhost:3050/api app.get('/api', (req,res) => { res.send({ hi: 'there' }); }); module.exports = app;
-
run the server:
node index.jd
-
Mongo DB Tips:
- MongoDB has fantastic support of geo based queries
- helpful package to make fake HTTP requests:
npm install supertest