Aller au contenu

MongoDB Node Driver

Le pilote officiel MongoDB Node.js permet aux applications Node.js de se connecter à MongoDB et de travailler avec des données. Le pilote dispose d'une API asynchrone qui vous permet d'accéder aux valeurs de retour de méthode via Promises ou de spécifier des rappels pour y accéder lors de la communication avec MongoDB. (Traduction du site officiel)

Étape 1 - Ajout du module mongodb

  1. Créez un nouveau projet Node
  2. L’initialiser
  3. Ajoutez le module mongodb :

    console
    npm install mongodb
    
  4. Importer le module dans le repo approprié :

    repo.ts
    import { MongoClient } from 'mongodb';
    

Étape 2 – Établissement de la connexion

mongo-connection

Décortiquer une chaîne de connection MongoDB
mongodb.ts
// connexion distante
const uri = "mongodb+srv://sample-hostname:27017/?poolSize=20&writeConcern=majority";

// connexion locale
const uri = "mongodb://localhost:<port>"

Étape 3 – Exécuter vos requêtes

Exemple : Utilisation dans une application Node.js (sans Express)

connexion.ts
import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection string.
const uri = "mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&writeConcern=majority";
const client = new MongoClient(uri);
async function run() {
  try {
    await client.connect();
    const database = client.db('sample_mflix');
    const movies = database.collection('movies');
    // Query for a movie that has the title 'Back to the Future'
    const query = { title: 'Back to the Future' };
    const movie = await movies.findOne(query);
    console.log(movie);
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);

Exemples d'utilisation

MongoDB cursor

Lorsque la requête retourne plusieurs résultats, ceux-ci sont retournés dans un curseur

Les fonctions suivantes retournent un curseur :

  • Collection.find()
  • Collection.aggregate()
  • Collection.listIndexes()
  • Db.listCollections()
  • Db.aggregate()

Voici deux façons de parcourir un curseur :

asynciterator.ts
const cursor = collection.find({});
console.log("async 1");

for await (const doc of cursor) {
  console.log(doc);
}
console.log("async 2");
manuelle.ts
const cursor = collection.find({});

while (await cursor.hasNext()) {
  console.log(await cursor.next());
}

La méthode toArray() permet d’obtenir un tableau à partir des documents d’un curseur. Cursor.toArray()

Express et MongoDB Node Driver (HTML)

express.ts
router.get('/', async (req, res) => {
        var html = '<h1>Liste des films</h1>'
        const client = new MongoClient(uri);
        try {
            await client.connect();
            const db = client.db('nom de la BD');
            const filmsCollection = db.collection('films');
    
            html += '<table>';   
        
            await filmsCollection.find({}).forEach(function(doc) {
                console.log(doc);
                html += '<tr>';        
                html += '<td>' + doc.titre + '</td>';           
                html += '<td>';        
                html += '<a href="/edit/' + doc._id + '">Modifier (à venir)</a>';        
                html += ' | ';        
                html += '<a href="/delete/' + doc._id + '">Supprimer (à venir)</a>';        
                html += '</td>';        
                html += '</tr>';  
            }); 
            
            html += '</table>';
        } finally {
            await client.close();
            res.send(html);
        }
    })

Express et MongoDB Node Driver (json)

express-json.ts
router.get('/', async (req: IReq, res: IRes) => {  
  
  const client = new MongoClient(uri);
  try {
    await client.connect();
    const db = client.db('nom de la BD');
    const listeFilms = await db.collection('films').find({}).toArray();
    res.json(listeFilms);
  } catch(err) {
    console.log(err.message);
    res.status(500).json({erreur: "Une erreur est survenue, veuillez contacter votre administrateur"})
  } finally {
    await client.close();
  }
  
});

Utilisation d'une interface pour la collection

movie.ts
    /// Code généré par : OpenAI. (2023). ChatGPT (version 3 août 2023) [Modèle massif 
    /// de langage]. https://chat.openai.com/chat
export interface IMovie {
  _id: {
    $oid: string;
  };
  plot: string;
  genres: string[];
  runtime: {
    $numberInt: string;
  };
  cast: string[];
  num_mflix_comments: {
    $numberInt: string;
  };
  title: string;
  fullplot: string;
  countries: string[];
  released: {
    $date: {
      $numberLong: string;
    };
  };
  directors: string[];
  rated: string;
  awards: {
    wins: {
      $numberInt: string;
    };
    nominations: {
      $numberInt: string;
    };
    text: string;
  };
  lastupdated: string;
  year: {
    $numberInt: string;
  };
  imdb: {
    rating: {
      $numberDouble: string;
    };
    votes: {
      $numberInt: string;
    };
    id: {
      $numberInt: string;
    };
  };
  type: string;
  tomatoes: {
    viewer: {
      rating: {
        $numberInt: string;
      };
      numReviews: {
        $numberInt: string;
      };
      meter: {
        $numberInt: string;
      };
    };
    lastUpdated: {
      $date: {
        $numberLong: string;
      };
    };
  };
}
tableau_de_fruits.ts
import { MongoClient } from 'mongodb';
import { IMovie } from './movie';

// Replace the uri string with your MongoDB deployment's connection string.
const uri = 'mongodb://localhost:27017/?readPreference=primary&ssl=false';
const client = new MongoClient(uri);
async function run() {
  try {
    await client.connect();
    const database = client.db('sample_mflix');
    const movies = database.collection<IMovie>('movies'); // Query for a movie that has the title 'Back to the Future'
    const query = { title: 'Back to the Future' };
    const movie = await movies.findOne(query);
    console.log(movie);
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);

Note

Les bases de données utilisées dans cette leçon proviennent de ce dépôt GitHub