Skip to content

Instantly share code, notes, and snippets.

@caprosset
Last active April 23, 2020 13:42
Show Gist options
  • Save caprosset/f7f1ff94279bc6e41a1a83afd8cdacca to your computer and use it in GitHub Desktop.
Save caprosset/f7f1ff94279bc6e41a1a83afd8cdacca to your computer and use it in GitHub Desktop.
Mongoose recipes - LAB review jan 2020
const mongoose = require("mongoose");
const Recipe = require("./models/Recipe.model"); // Import of the model Recipe from './models/Recipe.model.js'
const recipesJS = require("./data.js"); // Import of the data from './data.js'
// Connection to the database "recipeApp"
mongoose
.connect("mongodb://localhost/recipe-app-dev", {
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(x =>
console.log(`Connected to Mongo! Database name: "${x.connections[0].name}"`)
)
.catch(err => console.error("Error connecting to mongo", err));
/*
// Iteration 2
const recipe1 = {
title: "Lasagna 123",
level: "Amateur Chef",
ingredients: [
"1/2 cup rice vinegar",
"5 tablespoons honey",
"1/3 cup soy sauce (such as Silver Swan®)",
"1/4 cup Asian (toasted) sesame oil",
"3 tablespoons Asian chili garlic sauce",
"3 tablespoons minced garlic",
"salt to taste",
"8 skinless, boneless chicken thighs"
],
cuisine: "Asian",
dishType: "Dish",
image: "https://images.media-allrecipes.com/userphotos/720x405/815964.jpg",
duration: 40,
creator: "Bob"
};
// Recipe.create(recipe1, function (err, data) {
// if (err) console.log(err);
// else console.log(data);
// });
Recipe.create(recipe1)
.then(result => {
console.log("Created one recipe", result);
})
.catch(err => console.log(err));
*/
// Interation 3
// Recipe.insertMany(recipesJS, function (err, data) {})
Recipe.insertMany(recipesJS)
.then(createdRecipesArr => {
console.log("inserted documents", createdRecipesArr.length);
const pr = Recipe.updateOne(
{ title: "Rigatoni alla Genovese" },
{ duration: 100 },
{ new: true }
);
return pr;
})
.then(updatedRecipe => {
console.log("updated document", updatedRecipe);
const deletePr = Recipe.deleteOne({ title: "Carrot Cake" });
return deletePr;
})
.then(deletedStatusObj => {
console.log("deleted documents:", deletedStatusObj.deletedCount);
// After that close the connection
mongoose.connection.close();
})
.catch(err => console.log(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment