Side Note: Robo 3T - Better than Compass
modeling rations
querying data
Were generated using a export utility
From inside the folder where the JSON files you want to import are located:
mongoimport --db databasName --collection collectionName --file nameOfFile.json
A film has a collection of: - Characters - Planets - Specie - Starship - Vehicle Also has title and prodcer and director - If we had a director collection, could point to director property from another collection There's a relationship between the species and the planet One planet can have more than one species, but species can only have one planet - Same with characters (only one homeworld)
More than one but not many -
[Book] 1 --- _ [Author] 1 --- _ [Book] When relationship goes both ways:
const ObjectId = mongoose.Schema.Types.ObjectId
const definition = {
starship_class: {
type: String,
required: true
},
hyperdrive_rating: String,
key: Number,
pilots: [
{
type: ObjectId
}
]
}
Luis wrote code that grabbed that ID and turned it into ObjectID Don't need pilot key anymore, can take pilots (array of ObjectIDs) instead
Start with ref (linking)
Start with embedding (sub-documents)
Could be embedded or linked (ref)
Many would be linked, few embedded
const characterSchema = new mongoose.Schema({
name: {
type: String,
required: true,
index: true
},
gender: String,
height: {
type: Number,
min: 0
},
eye_color: String,
homeworld: {
type: ObjectId,
ref: 'Planet'
}
ship: [
{
type: ObjectId,
ref: 'Starship'
}
],
address: {
city; String,
state: String,
streetAddress: String
} <-- This address is an example of embedding
})
const character = mongoose.model('Character', characterSchema);
const definition = {
starship_class: {
type: String,
required: true
},
hyperdrive_rating: String,
key: Number,
pilots: [
{
type: ObjectId,
ref: 'Character'
}
]
};
const options = {
strict: false,
};
Embed document
Before you call .then, just a promise Every query can be captured as a query
let query = Character.find()
query.sort('name') // by name asending
query.sort('-name') // by name descending
query.sort('gender -height) // multiple fields
query.sort({ gender: 1, height: -1 }) // same as above
Have a lot of things but only need a few fields:
query.select('name gender')
query.select({ name: 1, gender: 1 })
query.select({_id: 0}) // exclude only _id
query.select({ name: 1, gender: 1, _id: 0 }) // excludes _id
Character.find({ gender: 'female'})
.sort('height')
.select('name')
.then().catch()
Could have query, don't execute it, and add to it
// incrementally building query:
const gender = req.query.gender;
let query = Characters.find()
if (gender) {
query.where({ gender: gender })
}
query.then(chars => res.json(chars)).catch();
query.where('age').gte(18).lte(62)
gte - greater than or equal to lte - less than or equal to
Normally people will not come to you with queries, they will come to you with questions:
You could see Gary Kurtz and it'll add that + for you. (turns to %20, which is equivalent)
When I say 'add movies property', I don't mean add to the schema, I mean add it to the response