Title: Mongoose Vampires
Type: Lab + Homework
Duration: 1 + hours
Creator: WDI-Meeseeks
Adapted by: Kristyn Bryan, Karolin Rafalski
For this lab, you will be using some of the mongoose commands that you learned today and you will be reading the documents to find new queries to complete the following activities. Researching queries and implementing them is a big part of this lab!
Utilize the following resources to research the commands you will need:
- Your notes from today
- Mongoose Documentation
- Google to find Stack Overflow articles and more
- Start your mongo server with
brew services start mongodb
in terminal - Navigate to this directory in terminal and
npm i
to installmongoose
- Open the project in code, you'll be working with some starter code in the
models
folder and theapp.js
file
Lets design a schema using mongoose and then use it to create some documents and eventually query for those documents.
Our vampire collection will look something like this:
var vampire = {
name: 'Chocula',
title: 'Count',
hair_color: 'brown',
eye_color: 'brown',
dob: new Date(1971, 2, 13, 7, 47),
loves: ['cereal','marshmallows'],
location: 'Minneapolis, Minnesota, US',
gender: 'm',
victims: 2
}
-
Build a vampire schema and model that matches the object above inside the
models/vampires.js
file -
Go to the Mongoose documentation to learn more about validations and defaults: http://mongoosejs.com/docs/api.html
-
The name field is required, so make sure that the schema accommodates for that.
-
Also, no vampire will have less than 0 victims, so add that into the schema as a validation.
-
Lastly, set the default value of the hair color to blonde.
Insert into the database using create method:
-
We have required
seed_vampires.js
for you in theapp.js
file. This is an array of Vampires for you to insert into your database. -
Write this command and run it ONCE in
app.js
- once you are done, comment it out or else every time you run it it will make duplicates of your data.Vampire.insertMany(seedData, (err, vampires) => { if (err){ console.log(err)} console.log("added provided vampire data", vampires) mongoose.connection.close(); });
-
Remember run your file with
node app.js
- Using the create method, create 4 new vampires with any qualities that you like two should be male and two should be female.
Write a different query for each of the following:
- Find all the vampires that that are females
- have greater than 500 victims
- have fewer than or equal to 150 victims
- have a victim count is not equal to 210234
- have greater than 150 AND fewer than 500 victims
Select all the vampires that:
- have a key of 'title'
- do not have a key of 'victims'
- have a title AND no victims
- have victims AND the victims they have are greater than 1000
Select all the vampires that:
- are from New York, New York, US or New Orleans, Louisiana, US
- love brooding or being tragic
- have more than 1000 victims or love marshmallows
- have red hair or green eyes
Select all the vampires that:
- love either frilly shirtsleeves or frilly collars
- love brooding
- love at least one of the following: appearing innocent, trickery, lurking in rotting mansions, R&B music
- love fancy cloaks but not if they also love either top hats or virgin blood * Hint-You will also have to use $nin *
Select all vampires that:
- love ribbons but do not have brown eyes
- are not from Rome
- do not love any of the following: [fancy cloaks, frilly shirtsleeves, appearing innocent, being tragic, brooding]
- have not killed more than 200 people
- replace the vampire called 'Claudia' with a vampire called 'Eve'. 'Eve' will have a key called 'portrayed_by' with the value 'Tilda Swinton'
- replace the first male vampire with another whose name is 'Guy Man', and who has a key 'is_actually' with the value 'were-lizard'
- Update 'Guy Man' to have a gender of 'f'
- Update 'Eve' to have a gender of 'm'
- Update 'Guy Man' to have an array called 'hates' that includes 'clothes' and 'jobs'
- Update 'Guy Man's' hates array also to include 'alarm clocks' and 'jackalopes'
- Rename 'Eve's' name field to 'moniker'
- We now no longer want to categorize female gender as "f", but rather as fems. Update all females so that the they are of gender "fems".
- Remove a single document wherein the hair_color is 'brown'
- We found out that the vampires with the blue eyes were just fakes! Let's remove all the vampires who have blue eyes from our database.
-
Check out Mongoose's Query Builder!
Person. find({ occupation: /host/ }). where('name.last').equals('Ghost'). where('age').gt(17).lt(66). where('likes').in(['vaporizing', 'talking']). limit(10). sort('-occupation'). select('name occupation'). exec(callback);
-
Write what that does in English:
Find a person whose occupation is ...
-
Make an index route that will res.send the json of all the data in our database.
-
If number 1 was easy, try to connect your database to your application and show a proper index page that displays your vampire data. If this is also easy, create a show page as well where you are showing individual vampire data.
-
Have extra time? Try out a few more problems on CodeWars