dupontL3alt
Projet final
Tutorial "Local Library" website written in Node/Express.
🎉code → https://github.com/dupontdenis/library.git
http://localhost:3000/catalog
http://localhost:3000/catalog/books
http://localhost:3000/catalog/author/create
Mongoose !
📕Cours
→ https://dupontmongodb.blogspot.com/
🛟Tout en 1
Installation Mongodb ⚒️
mongodb →
🥷Code
https://github.com/dupontdenis/testMongo.git
💸Code avec mongoose
https://github.com/dupontdenis/TESTMONGOOSE.git
Test
https://github.com/dupontdenis/TESTCORSMONGO.git
Let's start
Mongoose !
Example :
- // Include virtuals when converting documents to objects/JSON
- pizzaSchema.set("toObject", { virtuals: true });
- pizzaSchema.set("toJSON", { virtuals: true });
- // Virtual populate: connect Pizza -> Topping via Topping.pizzas (inverse relation)
- pizzaSchema.virtual("toppings", {
- ref: "Topping",
- localField: "_id",
- foreignField: "pizzas",
- justOne: false,
- });
- // Virtual total price (cents) includes toppings when `toppings` is populated
- pizzaSchema.virtual("totalPriceCents").get(function () {
- // Compute total price from populated `toppings` only (no base pizza price)
- if (
- !this.toppings ||
- !Array.isArray(this.toppings) ||
- this.toppings.length === 0
- )
- return 0;
- return this.toppings.reduce((acc, t) => acc + (t.priceCents || 0), 0);
- });
- pizzaSchema.virtual("totalPriceEur").get(function () {
- return (this.totalPriceCents || 0) / 100;
- });
- export const Pizza = mongoose.model("Pizza", pizzaSchema);
Relation : many to one
Inscription à :
Commentaires (Atom)