Driver Basics

Driver Basics: Connecting from Node: MongoClient and the Connection String, From mongosh to the Driver: find, insertOne and toArray.

Updated

  1. Connecting from Node: MongoClient and the Connection String (Journeyman): The official mongodb package connects a Node app to MongoDB through a MongoClient made from a connection string, which lives in a .env file that Node reads with --env-file, and the app connects that one client when it starts and closes it when it stops.
  2. From mongosh to the Driver: find, insertOne and toArray (Journeyman): Every mongosh query has a twin in the Node.js driver, written with db.collection("pets"), await, a projection inside an options object, toArray() to read a cursor, and new ObjectId(id) imported from the driver, and insertOne gives back the new document's insertedId.