without populate

export async function getBooks(req, res) {
  const books = await Book.find().populate("authors");
  res.json(books);
}

What happens without populate: book.authors is an array of ObjectId strings. The UI must resolve those ids to author details itself (or ask the server to).

Naive client approach: map ids -> N requests (one per author). Works but is slow and causes N+1 requests.

Example (works, but not optimal):

// book.authors = ['691c6...', '691d...']
const names = await Promise.all(
  book.authors.map(id =>
    fetch(`/authors/${id}`).then(r => {
      if (!r.ok) throw new Error('author fetch failed');
      return r.json();
    }).then(a => a.name)
  )
);
book.authors = names; // replace ids with names

Better: batch on the server — add an endpoint that accepts multiple ids and returns the authors in one query:

// GET /authors?ids=691c6...,691d...
// Server (Express + Mongoose)
router.get('/authors', async (req, res) => {
  const ids = (req.query.ids || '').split(',').filter(Boolean);
  const authors = await Author.find({ _id: { $in: ids } }, 'name'); // only name field
  res.json(authors);
});

Client side:

const ids = book.authors.join(",");
const resp = await fetch(`/authors?ids=${encodeURIComponent(ids)}`);
const authors = await resp.json();
const map = new Map(authors.map((a) => [a._id, a.name]));
book.authors = book.authors.map((id) => map.get(id) || null);


Bilan : Keep using populate server-side so the UI gets names in one response.

  • Server-side aggregation to return books with only needed author fields.
  • populate('authors', 'name') server-side so the UI gets names in one response.

*Transformer APP en API

📦 Blog Posts API (HTML + JSON)

This project serves blog posts two ways:

  1. Traditional server-rendered HTML pages (SSR)
  2. A clean JSON API you can consume with fetch() from any frontend

The goal: show the difference between an "application" that returns HTML and an "API" that returns raw data — and let you easily build your own client.

Git 🚀 https://github.com/dupontdenis/mongoose-API-virtual.git


API UI (Read-Only)

A minimal, dependency-free web UI to view posts from a REST API.

Supported endpoints:

  • GET /api/posts — list all posts
  • GET /api/posts/:id — get one post by ID

Git 🚀https://github.com/dupontdenis/API-Viewer.git

Methodes et Virtual in shema

🎓 Mongoose Virtual & Method Mini Demo

Minimal teaching project to show two core Mongoose schema features:

  1. virtual property url (computed, not stored in MongoDB)
  2. An instance method getSummary(maxLength) (logic with parameters)

🚀 https://github.com/dupontdenis/mongoose-virtual.git

Agregation

Tiny Mongoose walkthrough that models a simple university example and demonstrates two ways to compute an average: an instance method on the document and a MongoDB aggregation pipeline.

git 🚀 https://github.com/dupontdenis/mongoose-agregation.git

API with mongoose !

 

🎯 Learning Objectives

  • Understand REST API principles
  • Learn HTTP methods: GET, POST, PUT, PATCH, DELETE
  • Work with Express.js and MVC pattern
  • Connect to MongoDB
  • Interact with the API using Fetch, curl, or the VS Code REST Client


git 🛩️ https://github.com/dupontdenis/let-s-start-mongoAPI.git


🚀Applications pour tester l'API !

 Pensez à bien lancer le serveur dans un terminal


Simple : https://dupontdenis.github.io/testAPI/


https://dupontdenis.github.io/useAPIBLOGSIMPLE/

Let's start with mongoose !

🎯 Learning Objectives

git 🛩️https://github.com/dupontdenis/introMongoose.git

This repository is a **hands-on tutorial** designed to teach you the fundamentals of Mongoose, the popular MongoDB object modeling library for Node.js. Through practical examples, you'll learn how to: 

many to one !

git 🛩️

This repository is designed to teach you about two different ways to implement many-to-one relationships in MongoDB using Mongoose. We'll use a classic example: Authors and Books.

 https://github.com/dupontdenis/mongoose-one-two-many.git


Validation avec mongoose

Contrairement à mongodb qui ne permet pas de valider un schéma mongoose propose de nombreuses option de validation

lire : 🚀La validation Mongoose

import CVS

 Follow the Database Tools Installation Guide to install mongoimport.

😈 Run mongoimport from the system command line, not the mongo shell.


Example

🪛Lancez un terminal et ouvrir VisualStudio


>code .

🪛Créez un fichier File1.csv 
name,num,email
you,20240001,you@univ-evry.fr
me,20240002,me@univ-evry.fr

🪛Dans le terminal exécutez la commande

C:\Users\DD>mongoimport --type csv --headerline --db MyFirstDatabase --collection students --file File1.csv

🚀Verification



🪛Avec mongosh

> db.students.find()
[
  {
    _id: ObjectId("65469262c769f35c5f4be3fe"),
    name: 'you',
    num: 20240001,
    email: 'you@univ-evry.fr'
  },
  {
    _id: ObjectId("65469262c769f35c5f4be3ff"),
    name: 'me',
    num: 20240002,
    email: 'me@univ-evry.fr'
  }
]
>