Mongo
Basics
Command |
Description |
mongo |
start the MongoDB shell |
mongod |
start the MongoDB server |
mongos |
start the MongoDB sharded cluster router |
Database Operations
Operation |
Description |
use <database> |
switch to a database |
show dbs |
show a list of databases |
db.<collection>.insert() |
insert a document into a collection |
db.<collection>.find() |
find documents in a collection |
db.<collection>.update() |
update documents in a collection |
db.<collection>.remove() |
remove documents from a collection |
db.<collection>.aggregate() |
perform aggregation operations on a collection |
Query Operators
Operator |
Description |
$eq |
matches values that are equal to a specified value |
$ne |
matches values that are not equal to a specified value |
$gt |
matches values that are greater than a specified value |
$lt |
matches values that are less than a specified value |
$gte |
matches values that are greater than or equal to a specified value |
$lte |
matches values that are less than or equal to a specified value |
$in |
matches any of the values specified in an array |
$nin |
matches none of the values specified in an array |
$and |
joins query clauses with a logical AND |
$or |
joins query clauses with a logical OR |
$not |
inverts the effect of a query expression |
$exists |
matches documents that have a specified field |
Aggregation Pipeline Stages
Command |
Description |
$match |
filters documents based on specified criteria |
$group |
groups documents by a specified field |
$sort |
sorts documents by a specified field |
$skip |
skips a specified number of documents in a collection |
$limit |
limits the number of documents returned from a collection |
$project |
specifies the fields to include or exclude from a document |
$unwind |
deconstructs an array field into individual documents |
$lookup |
performs a left outer join with another collection |
$out |
writes the results of an aggregation pipeline to a collection |
Examples
Creating a database
Creating a collection
db.createCollection('students');
Inserting documents into a collection
db.students.insertOne({name: 'John Doe', age: 21, gender: 'M'});
db.students.insertMany([
{name: 'Jane Doe', age: 19, gender: 'F'},
{name: 'Bob Smith', age: 22, gender: 'M'}
]);
Updating documents in a collection
db.students.updateOne({name: 'John Doe'}, {$set: {age: 23}});
db.students.updateMany({gender: 'M'}, {$inc: {age: 1}});
Deleting documents from a collection
db.students.deleteOne({name: 'Bob Smith'});
db.students.deleteMany({gender: 'F'});
Finding documents in a collection
db.students.find();
db.students.find({gender: 'M'});
db.students.findOne({name: 'John Doe'});
Querying with operators
db.students.find({age: {$gt: 20}});
db.students.find({name: {$regex: /Doe/i}});
Sorting results
db.students.find().sort({age: -1});
Limiting results
db.students.find().limit(2);
Aggregating data
db.students.aggregate([
{$group: {_id: '$gender', avg_age: {$avg: '$age'}}}
]);
Indexing collections for faster queries
db.students.createIndex({name: 1});
Joining collections with $lookup
db.students.aggregate([
{
$lookup: {
from: 'grades',
localField: '_id',
foreignField: 'student_id',
as: 'grades'
}
}
]);