GoConqr has a new look! Let us know what you think.

Flashcards

W4: Mongo & mongoose

Emma Tysk Webdev English 21 cards

Cards in this deck (21)

  1. 1What is mongo?MongoDB is a NoSQL document-based database that stores data in JSON-like format.
  2. 2What is mongoose?It provides you with an API to help you interact with your MongoDB database
  3. 3Save a cafe to dbvar createCafe = function(req,res){
    var cafe = new Cafe({
    "name":req.body.name
    });
    cafe.save( (err,newCafe) => {
    if(!err) {
    res.send(newCafe);
    }else{
    res.sendStatus(400);
    }
    });
    };
  4. 4Find all cafésvar mongoose = require('mongoose');
    var Cafe = mongoose.model('cafes');
    var findAllCafes = function(req,res){
    Cafe.find({}, function(err,cafes){
    if(!err){
    res.send(cafes);
    }else{
    res.sendStatus(404);
    }
    });
    };
  5. 5find one café by IDvar mongoose = require('mongoose');
    var Cafe = mongoose.model('cafes');
    var findOneCafe = function(req,res){ Cafe.findById(req.params.id,function(err,cafe){
    if(!err){
    res.send(cafe);
    }else{
    res.sendStatus(404);
    }
    });
    };
  6. 6find café by namevar findCafeByName = function(req, res){
    Cafe.find({name: req.params.name}, function(err,cafe) {
    if(!err) {
    res.send(cafeName);
    }
    else{
    res.sendStatus(404);
    }
    });
    };
  7. 7update the user starlord55 to starlord 88// find the user starlord55
    // update him to starlord 88
    User.findOneAndUpdate({ username: 'starlord55' }, { username: 'starlord88' }, function(err, user) {
    if (err) throw err;
    // we have the updated user returned to us
    res.send(user);
    });
  8. 8Find and Remove a user by usernameUser.findOneAndRemove({ username: 'starlord55' }, function(err) {
    if (err) throw err;
    // we have deleted the user
    console.log('User deleted!');
    });
  9. 9Find By ID and Remove// find the user with id 4
    User.findByIdAndRemove(4, function(err) {
    if (err) throw err;
    // we have deleted the user
    console.log('User deleted!');
    })
  10. 10What is crud?Create, Read, Update, Delete
  11. 11find one by ID call structure.findById(id, [callback])
  12. 12Create a schema for a student with a name and idvar Schema = mongoose.Schema;
    var student = new Schema({
    name: {
    Type: String,
    Required: true,
    }
    id: {
    Type: Int,
    Required: true,
    }
    });
  13. 13What are the two ways to save to a db?var Tank = mongoose.model('Tank', yourSchema);
    var small = new Tank({ size: 'small' });
    small.save(function (err) {
    if (err) return handleError(err);
    // saved!
    });
    // or
    Tank.create({ size: 'small' }, function (err, small) {
    if (err) return handleError(err);
    // saved!
    });
  14. 14What is a mongoose model?Models are fancy constructors compiled from Schema definitions.
  15. 15What is the benefit of using MongoDB?1. Schema less − it is a document database in which one collection holds different documents. Number of fields, content and size of the document can differ from one document to another
    2. , it is very easy to install and setup
  16. 16Explain mongo documents & collectionsMongoDB is a database that stores your data as documents. Most commonly these documents resemble a JSON-like structure:
    {
    firstName: "Jamie",
    lastName: "Munro"
    }
    A document then is placed within a collection. As an example, the above document example defines a user object. This user object then would typically be part of a collection called users.
  17. 17Create a simple schema for a person with a name and height, no field is required. THEN: create a model of the schemavar mongoose = require('mongoose');
    var PeopleSchema = mongoose.Schema (
    {
    "name":String,
    “height":String,
    }
    );
    mongoose.model(‘people', PeopleSchema);
  18. 18Downside of mongodbno transactions
    no joins
  19. 19what are the two mongoose methods to remove and object from mongodb?findOneAndRemove()
    findByIdAndRemove()
  20. 20findOneAndUpdate call structurefindOneAndUpdate(conditions, new data, callback)
  21. 21Query for finding one or several documents (mongoose)findOne()
    example:
    Adventure.findOne({ type: 'iphone' }, function (err, adventure) {});
    findByID()
    // find adventure by id and execute
    Adventure.findById(id, function (err, adventure) {});
    find()
    MyModel.find({ name: 'john'})
    find all cafés with

Information

Description
Webdev Flashcards on W4: Mongo & mongoose, created by Emma Tysk on 09/06/2019.
Created by
Emma Tysk
Views
1
Rating

Feedback

What is it about?

What do you think of the new look?

How do you like it?