Flashcards
W4: Mongo & mongoose
Cards in this deck (21)
- 1What is mongo?MongoDB is a NoSQL document-based database that stores data in JSON-like format.
- 2What is mongoose?It provides you with an API to help you interact with your MongoDB database
- 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);
}
});
}; - 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);
}
});
}; - 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);
}
});
}; - 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);
}
});
}; - 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);
}); - 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!');
}); - 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!');
}) - 10What is crud?Create, Read, Update, Delete
- 11find one by ID call structure.findById(id, [callback])
- 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,
}
}); - 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!
}); - 14What is a mongoose model?Models are fancy constructors compiled from Schema definitions.
- 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 - 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. - 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); - 18Downside of mongodbno transactions
no joins - 19what are the two mongoose methods to remove and object from mongodb?findOneAndRemove()
findByIdAndRemove() - 20findOneAndUpdate call structurefindOneAndUpdate(conditions, new data, callback)
- 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