Lesson 26 +10 XP

MongoDB Drivers

MongoDB Drivers

mongosh is great for working in the terminal, but real applications talk to MongoDB through language drivers. Drivers use the same methods you already know - find(), insertOne(), aggregate() - right from your programming language.

Official drivers

MongoDB provides officially supported drivers for 12 languages:

  • C
  • C++
  • C#
  • Go
  • Java
  • Node.js
  • PHP
  • Python
  • Ruby
  • Rust
  • Scala
  • Swift

Plus community-supported libraries for many more languages.

The API stays the same

Whether you use the Node.js driver, PyMongo, or mongosh, the operations look familiar:

// insert
collection.insertOne({ title: "Hello" })

// read
collection.findOne({ title: "Hello" })

// update
collection.updateOne({ title: "Hello" }, { $set: { likes: 5 } })

// delete
collection.deleteOne({ title: "Hello" })

// aggregate
collection.aggregate([{ $match: { likes: { $gt: 1 } } }])

Which driver to pick

Use the official driver for your language. The rest of this module focuses on the Node.js driver.

TL;DR

  • Applications use language drivers, not mongosh.
  • 12 officially supported languages, plus community libraries.
  • Driver methods mirror the mongosh CRUD API.