Photo by PJH on Unsplash

On this page

This chapter follows Vector search and embeddings in MongoDB. For narrative context, start with A short history of MongoDB.

mongosh

Command Purpose
show dbs List databases
use mydb Switch database context
show collections List collections
db.collection.find().limit(5) Sample query
db.collection.getIndexes() List indexes
db.runCommand({ ping: 1 }) Health check

Query operators

Operator Meaning
$eq, $ne Equal / not equal
$gt, $gte, $lt, $lte Comparisons
$in, $nin Array membership
$and, $or, $nor Boolean logic
$exists Field presence
$regex Pattern match
$text Text index search
$near, $geoWithin Geospatial (indexed)

Update operators

Operator Meaning
$set, $unset Set or remove fields
$inc Numeric increment
$push, $pull, $addToSet Array updates
$rename Rename field
$currentDate Set date fields

Aggregation stages (common)

Stage Role
$match Filter
$project Shape fields
$group Aggregate
$sort, $limit, $skip Order / slice
$lookup Join
$unwind Expand arrays
$facet Parallel pipelines
$merge / $out Persist results
$search Atlas Search
$vectorSearch Vector Search

Index types

Index Use
Single / compound Equality, sort, range
Multikey Array fields
Unique Enforce uniqueness
Partial Index subset of docs
TTL Expire documents
2dsphere GeoJSON
Text $text queries

Node.js driver (mongodb package)

Task API
Connect new MongoClient(uri).connect()
Insert collection.insertOne, insertMany
Find find, findOne
Update updateOne, updateMany, replaceOne, findOneAndUpdate
Delete deleteOne, deleteMany
Batch writes bulkWrite
Aggregate collection.aggregate(pipeline)
Index createIndex, createIndexes
Transaction session.withTransaction(fn)

Next: MongoDB glossary.

Search articles

Type a word to search published articles.

Open the search page