Giter VIP home page Giter VIP logo

mongoose-vampires-hw's Introduction

GA Logo

Mongoose Vampires

For this two part homework assignment, you will be using some mongoose commands and you will be reading documentation to find new queries/techniques to complete the following activities. Researching queries and implementing them is a big part of this homework!

mongoose

Resources

Utilize the following resources to research the commands you will need:

Setup

  1. Fork and clone this repo, which includes:
  • A file for writing your app called server.js. You will write your code in here (even for the database).

  • Don't forget to install dependencies: npm i

Comment out each database query once you get it working so that you're only running one at a time. This is where we will be looking for your work after you turn it in.

  • A file called populateVampires.js that includes data on vampires that you will add (later).
  1. Install mongoose with npm. Require it in server.js.

What is a schema?

A schema is a way to organize, ahead of time, what a group of data is going to look like. This can be at various levels of a database depending on what kind of databases you are using.

Mongo, is schema-less on the database level. It doesn't care what the data looks like and will take in virtually anything as long as it's syntactically correct.

Why they are important?

Even when you are using MongoDB, an inherently schema-less database, a schema can be very helpful. It helps control what is going into the database so that you can both know what is going into it, and to make validations. Note that with MongoDB, even if a piece of data is not a part of your original schema, you can still store it.

Mongoose

This is where mongoose comes in. Instead of manually making sure everything we are putting into our database makes sense and conforms to some type of structure, Mongoose allows us to define schemas.

Mongoose, in the background, can enforce these schemas (as strictly as you like) in order to make sense of the data going into the database and to allow validation. It provides powerful and simple to use tools to do this.

Building a Schema

Lets design a schema using mongoose and then use it to create some documents and eventually query for those documents.

  1. Create a folder inside your project called models.

  2. Create a file inside your models folder called vampire.js (singular). You will create your schema and model in this file.

  3. Look back at the class notes to see how to start your schema; use the Article model as a guide.

A typical object in our vampire collection will look something like this:

const vampire = {
  name: 'Count Chocula',
  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,
}
  1. Build a vampire schema and model that matches the object above. Export your model.

Pause. Take a minute to do a little research and come up with an answer to this question: What's the difference between a Schema and a Model?

  1. Go to the Mongoose documentation to learn more about validations and defaults: http://mongoosejs.com/docs/api.html

  2. The name field is required, so make sure that the schema accommodates for that.

  3. Also, no vampire will have less than 0 victims, so add that into the schema as a validation.

  4. Lastly, set the default value of the hair color to blonde.

  5. Set up your server.js file to connect to your vampires database.

  6. If you like, you may now try testing your schema with the automated tests by running npm test in your terminal.


๐Ÿ”ด Commit. Suggested message: "made a schema"

Add the vampire data that we gave you

There's an array of "vampire" JavaScript objects in populateVampires.js, and we need to add them add the vampires to a mongoDB vampires collection.

In class, you inserted one object using the .create() method on your model.

I wonder if you could insert an array of vampires using the Vampire.create() model method? Maybe just by passing in an array? Perhaps you already tried it.

Or!

Fun fact: you can access native mongoDB methods in an object on the model: .collection

Maybe you can do this simply by providing this array to insert method and it will create a document for each object in the array?

If you're stuck, click below

Vampire.collection.insertMany(vampireData, (err, data) => {
  console.log("added provided vampire data")
  mongoose.connection.close();
});

Part 1:

Add some new vampire data

Using the Vampire.create() method, create 4 new vampires with any qualities that you like two should be male and two should be female.


๐Ÿ”ด Commit. Suggested message: "added data into vampire collection"

Querying

Select by comparison

Write a different query for each of the following:

  1. Find all the vampires that that are females
  2. have greater than 500 victims
  3. have fewer than or equal to 150 victims
  4. have a victim count is not equal to 210234
  5. have greater than 150 AND fewer than 500 victims

๐Ÿ”ด Commit. Suggested message: "queried for vampires"

Select by exists or does not exist

Select all the vampires that:

  1. have a title property
  2. do not have a victims property
  3. have a title AND no victims
  4. have victims AND the victims they have are greater than 1000

๐Ÿ”ด Commit. Suggested message: "selected vampires"

Select with OR

Select all the vampires that:

  1. are from New York, New York, US or New Orleans, Louisiana, US
  2. love brooding or being tragic
  3. have more than 1000 victims or love marshmallows
  4. have red hair or green eyes

๐Ÿ”ด Commit. Suggested message: "selected more vampires with OR"

Part 2

Before you continue on to part two, you should know that Mongoose has some sweet helper functions that can make all this a little easier. See below.

Mongoose's default find gives you an array of objects. But what if you know you only want one object? These convenience methods just give you one object without the usual array surrounding it.

Article.findById('5757191bce5579b805705900', (err, article)=>{
  console.log(article);
});
Article.findOne({ author : 'Matt' }, (err, article)=>{
  console.log(article);
});
Article.findByIdAndUpdate(
  '5757191bce5579b805705900', // id of what to update
  { $set: { author: 'Matthew' } }, // how to update it
  { new : true }, // tells findOneAndUpdate to return modified article, not the original
  (err, article)=>{
    console.log(article);
  });
});
Article.findOneAndUpdate(
  { author: 'Matt' }, // search criteria of what to update
  { $set: { author: 'Matthew' } }, // how to update it
  { new : true }, // tells findOneAndUpdate to return modified article, not the original
  (err, article)=>{
    console.log(article);
  });
});
Article.findByIdAndDelete('5757191bce5579b805705900', (err, article)=>{
  console.log(article); // log article that was removed
});
Article.findOneAndDelete({ author : 'Matt' }, (err, article)=>{
  console.log(article); // log article that was removed
});

6. Select objects that match one of several values

Select all the vampires that:

  1. love either frilly shirtsleeves or frilly collars
  2. love brooding
  3. love at least one of the following: appearing innocent, trickery, lurking in rotting mansions, R&B music
  4. love fancy cloaks but not if they also love either top hats or virgin blood * Hint-You will also have to use $nin *

๐Ÿ”ด Commit. Suggested message: "selected even more vampires"

7. Negative Selection

Select all vampires that:

  1. love ribbons but do not have brown eyes
  2. are not from Rome
  3. do not love any of the following: [fancy cloaks, frilly shirtsleeves, appearing innocent, being tragic, brooding]
  4. have not killed more than 200 people

๐Ÿ”ด Commit. Suggested message: "used negative selections on vampire data"

You made it! You completed the homework!

Hungry for More

Replace

  1. Replace the vampire called 'Claudia' with a vampire called 'Eve'.

๐Ÿ”ด Commit. Suggested message: "replaced vampire data"

Update

  1. Update 'Eve' to have a gender of 'm'
  2. Rename 'Eve's' name field to 'moniker'
  3. 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".

๐Ÿ”ด Commit. Suggested message: "updated vampire data"

Remove

  1. Remove a single document wherein the hair_color is 'brown'
  2. 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.

๐Ÿ”ด Commit. Suggested message: "remove vampire data"

Hungry for Even More

  1. Make an index route that will res.send() (or even better--res.json()--look it up!) the json of all the data in our database.

  2. 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.

  3. Have extra time? Try out a few more problems on CodeWars


๐Ÿ”ด Commit. Suggested message: "tackled some Hungry for More"

mongoose-vampires-hw's People

Contributors

h64 avatar jimbojones1 avatar michaelpetty avatar msolorio avatar reuben-ga avatar reubenayres avatar ryanfleharty avatar

Watchers

 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.