- MongoDB Cheat Sheet
db.collection.count();
This method counts and returns the number of results that match a query.
db.collection.deleteOne({ status: "D" });
Delete the first document that match a specified filter.
db.collection.deleteMany({ status : "A" });
Delete all documents that match a specified filter.
db.collection.deleteMany( {} );
Check if a field exists in the document.
- Use true to find a field that exists.
- Use false to find documents in which a field does not exist.
db.collection.find({ qty: { $exists: true } });
The query returns the documents that contain the field qty
, including documents where the field value is null
.
db.collection.find({ qty: { $exists: false } });
The query returns only the documents that do not contain the field qty
.
db.collection.find({});
Selects all documents in a collection.
db.collection.find({}, { name: 1 });
The second parameter (optional) specifies the fields to return in the document.
db.collection.find({ releaseYear: "2021" }, { title: 1, author: 1, _id: 0 });
Returns all documents that match the query, and display only the title
and author
.
db.createCollection( "minhaColecao4", { collation: { locale: "fr" } } );
Create a new collection.
db.collection.insertOne({ chave: "Valor", chave2: "Valor2" });
Insert only one document in the collection. If the collection doesn't exist, mongodb automatically creates it.
db.collection.insertMany([ { chave: "Valor", chave2: "Valor2" }, { chave: "Valor", chave2: "Valor2" } ]);
Insert more than one document in a collection.
db.collection.find("<query>").limit("<número>");
Specify the number of documents to be returned.
db.collection.find().skip(2);
Specify the number of documents to be skipped. It controls where mongodb will begin to return results.
db.collection.find({}).limit(10).skip(5);
Return ten results, skipping the first five documents, from the collection.
Sort items, numerically or alphabetically.
- Ascending order (Ex: sort: { key: 1 })
- Descending order (Ex: sort: { key: -1 })
db.collection.find({}).sort({ "price": 1 });
Sort the field price
in ascending order for all documents in a collection.
db.collection.find({ qty: { $lt: 20 } });
Returns all documents in which the field qty
is less than 20.
db.collection.find({ qty: { $lte: 20 } });
Returns all documents in which the field qty
is less than or equal 20.
db.collection.find({ qty: { $gt: 20 } });
Returns all documents in which the field qty
is greater than 20.
db.collection.find({ qty: { $gte: 20 } });
Returns all documents in which the field qty
is greater than or equal 20.
db.collection.find({ qty: { $eq: 20 } });
db.collection.find({ qty: 20 });
Returns all documents in which the field qty
is equal to 20.
db.collection.find({ qty: { $ne: 20 } });
Returns all documents in which the field qty
is not equal to 20.
db.collection.find({ qty: { $in: [ 5, 15 ] } });
Returns all documents in which the field qty
is equal to 5 or 15.
db.collection.find({ qty: { $nin: [ 5, 15 ] } });
Returns all documents in which the field qty
is not equal to 5 neither 15. It also returns documents in which the field qty
does not exist.
db.collection.find({ price: { $not: { $gt: 1.99 } } });
db.collection.find({ $or: [{ qty: { $lt: 20 } }, { price: 10 }] });
Executes a logical operator OR on an array of two or more query expressions, and returns the documents that satisfy at least one of them.
db.collection.find({ $nor: [{ price: 1.99 }, { sale: true }] });
Executes a logical operator NOR on an array of two or more query expressions, and returns the documents that fail at least one of them.
db.collection.find({
$and: [
{ price: { $ne: 1.99 } },
{ price: { $exists: true } }
]
});
Executes a logical operator AND on an array of two or more expressions, and returns the documents that satisfy all of them.
It is possible to combine the operators to create more complex query. The expression below returns all documents in the collection that have the field price
between 0.99 - 1.99 AND the field sale
true or the field qty
less than 20.
db.collection.find({
$and: [
{
$or: [
{ price: { $gt: 0.99, $lt: 1.99 } }
]
},
{
$or: [
{ sale : true },
{ qty : { $lt : 20 } }
]
}
]
});
Updates a single document based on the filter (the first one to match it). This method takes the following parameters:
- filter: the selection criteria for the update
- $set: The modifications that will be applied
- $upsert: When
true
, creates a new document if no documents match the filter. This parameter is optional.
db.collection.updateOne(
{ item: "paper" },
{ $set: { "size.uom": "cm", status: "P" } },
{ $upsert: true }
)
Updates all the documents that match the filter criteria. This method takes the same parameters as updateOne
.
db.collection.updateMany(
{ item: "paper" },
{ $set: { "size.uom": "cm", status: "P" } },
{ $upsert: true }
)
Multiply the value of a field by a number, updating the document. If the field does not exist, $mul creates it and sets the value to zero.
db.collection.updateOne(
{ _id: 1 },
{ $mul: { price: NumberDecimal("1.25"), qty: 2 } }
)
Increments (positive number) or decrements (negative number) a field by a specified value. If the field does not exist, $inc creates the field and sets the field to the specified value.
db.collection.updateOne(
{ sku: "abc123" },
{ $inc: { quantity: -2, orders: 10 } }
);
The $max sets the upper limit of a field. It compares the specified value with the field's value. If the current value is lower than the specified value, the document will be updated. Otherwise the field's value won't be changed.
db.collection.update({ _id: 1 }, { $max: { highScore: 950 } });
In the example above, if the current value is lower than 950 the $max will update the highScore
field to 950. If it is already higher than 950, nothing will happen.
The $min sets the lower limit of a field. It compares the specified value with the field's value. If the current value is greater than the specified value, the document will be updated. Otherwise the field's value won't be changed.
db.collection.update({ _id: 1 }, { $min: { lowScore: 150 } });
In the example above, if the current value is higher than 150 the $min will update the lowScore
field to 150. If it is already lower than 150, nothing will happen.
Update the name of a field.
db.collection.updateMany([
{},
{ $rename: {
"name": "productName"
}
}
]
);
In the example above, $rename will update the name
field to productName
in all documents.
Add one or more fields.
db.collections.update(
{ _id: 100 },
{ $set: {
quantity: 500,
details: { model: "14Q3", make: "xyz" },
tags: [ "coats", "outerwear", "clothing" ]
}
}
);
In the example above, $set adds the fields quantity
, details
and tags
in the documents with _id: 100
.
Remove one or more fields.
db.collection.updateMany(
{ productName: "Banana" },
{ $unset: { quantity: "" } }
);
In the example above, $unset deletes the field quantity
in the documents with productName: "Banana"
.
Set the value of a field to the current date, either as a Date or a timestamp.
db.customers.updateOne(
{ _id: 1 },
{
$currentDate: {
lastModified: true,
"cancellation.date": { $type: "timestamp" }
},
$set: {
"cancellation.reason": "user request",
status: "D"
}
}
)
The example above updates the lastModified field to the current date, the "cancellation.date" field to the current timestamp as well as updating the status field to "D" and the "cancellation.reason" to "user request".
The $addToSet
operator adds a value to an array only if it is not present. If the array already contains the value, the $addToSet
does nothing. It ensures that there are no duplicate items being added to the array.
db.collection.updateOne(
{ _id: 1 },
{ $addToSet: { tags: "accessories" } }
);
When multiple values are added. the method can be used with the operator $each
. In the example below, the items "camera" and "electronic" will be added to the array in the field tags
. It won't add "accessories" because this item already exists in the array.
db.collection.updateOne(
{ _id: 2 },
{
addToSet: {
tags: {
each: ["camera", "electronics", "accessories"]
}
}
}
);
db.collection.updateMany(
{},
{ $set : {
"ingredients.$[elemento].unit": "xícara",
"ingredients.$[elemento].name": "Farinha Integral"
}
},
{ arrayFilters: [ { "elemento.name": "Farinha" } ]}
);
db.collection.updateOne({ _id: 1 }, { $pop: { items: -1 } });
Removes the first item of an array in the field items
.
db.collection.update({ _id: 1 }, { $pop: { items: 1 } });
Removes the last item of an array in the field items
.
Removes items of an array if the item match the query condition.
db.collection.updateMany(
{},
{
pull: {
"items": {
"name": { $in: ["pens", "envelopes"] }
},
}
}
);
db.collection.updateOne(
{ _id: 1 },
{
pull: {
votes: { $gte: 6 }
}
}
);
db.collection.updateMany(
{},
{
pull: {
results: { score: 8 , item: "B" }
}
}
);
db.collection.updateOne(
{_id: 1 },
{ $push:
{
items: {
"name": "notepad",
"price": 35.29,
"quantity": 2
},
}
},
{ $upsert: true }
);
db.collection.updateOne(
{},
{ $push: {
items:
{ $each: [
{
"name": "pens",
"price": 56.12,
"quantity": 5
},
{
"name": "envelopes",
"price": 19.95,
"quantity": 8
}
] }
}
},
);
db.collection.updateOne(
{ _id: 1},
{ $push: {
items: {
$each: [
{
"name" : "notepad",
"price" : 35.29,
"quantity" : 2
},
{
"name": "envelopes",
"price": 19.95,
"quantity": 8
},
{
"name": "pens",
"price": 56.12,
"quantity": 5
}
],
$sort: { quantity: -1 },
$slice: 2
}
}
},
{ upsert: true }
);
Modifier: $each Add multiple values into an Array
Modifier: $slice Slice the array in a specific index (Ex: slice: 2)
- Must be used with $each
Modifier: $sort Sort items into the array.
- Ascending order (Ex: $sort: { key: 1 })
- Descending order (Ex: $sort: { key: -1 })
- Must be used with $each
Modifier: $position Insert item into a specific index
- Without it the new item will be inserted at the last position in the array
- Must be used with $each
Selects the documents where the value of a field is an array that contains all the specified elements.
db.inventory.find(
{ tags: { $all: ["red", "blank"] } }
);
Select documents that contain an array field with at least one element that matches all the specified filters.
db.collection.find(
{ results: { $elemMatch: { $gte: 80, $lt: 85 } } }
);
Select documents that match any array with the number of elements specified by the argument.
db.products.find(
{ tags: { $size: 2 } }
);
Use to pass either a string containing a JavaScript expression or a full JavaScript function.
Starting in MongoDB 3.6, the $expr operator allows the use of aggregation expressions.
If you must create custom expressions, $function is preferred over $where.
Allows the use of aggregation expressions. It compares fields from the same document.
db.monthlyBudget.find(
{
expr: { $gt: [ "$spent", "$budget" ] }
}
);
Provides regular expression capabilities for pattern matching strings.
db.products.find({ sku: { $regex: /789$/ } });
db.products.find({ sku: { $regex: /^ABC/i } });
Performs a text search on the content of the fields indexed with a text index.
{
$text:
{
search: <string>,
language: <string>,
caseSensitive: <boolean>,
diacriticSensitive: <boolean>
}
}
$search
: string of terms that MongoDB parses and uses to query the text index;
$language
: determines the list of stop words for the search and the rules for the tokenizer;
$caseSensitive
: boolean flag to enable or disable case sensitive search. Defaults to false;
$diacriticSensitive
: boolean flag to enable or disable diacritic sensitive. Defaults to false
Select documents where the value of a field divided by a divisor has the specified remainder.
db.inventory.find({ qty: { $mod: [4, 0] } });
Aggregation expressions use field path to access fields in the input documents. To specify a field path, prefix the field name with a dollar sign $.
The aggregation pipeline is a framework for MongoDB. Documents enter a multi-stage pipeline that transforms the documents into an aggregated result. Documents pass through the stages in sequence. Each stage transforms the documents as they pass from one stage to another.
db.collection.aggregate([
{ $match: { <query> } },
]);
Filters the documents so only those that match the specified condition(s) may enter the next stage. It resembles the filters from find() method.
db.collection.aggregate([
{ $match: { name: "Alex" } },
]);
db.collection.find({ name: "Alex" });
Select all documents that match the query.
This operator may be used at any stage of the pipeline. When used as the first stage, it should improve considerably the performace of your query. In the other hand, when used at any other point, it will act similar to a HAVING from the SQL.
db.collection.aggregate([
{ $limit: <positive integer> },
]);
Passes only the first n documents to the next stage in the pipeline, in which n is the specified positive integer.
db.collection.aggregate([
{ $match: { price: 10 } },
{ $limit: 5 },
]);
Returns the first five results that matches the specified condition from the previous stage.
This operator allows the use of documents from another collection (table), similar to a JOIN from the SQL. The documents from the joined collection will be avaible through a new array.
There are two types of $lookup
:
db.collection.aggregate([
{
$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
},
},
]);
db.collection.aggregate([
{
$lookup:
{
from: <inventory>,
localField: <item>,
foreignField: <sku>,
as: <inventory_docs>
},
},
]);
This operator groups documents by the specified _id
expression. It's worth mentioning that the specified _id
is actually a "parameter" of $group
and is not to be mistaken with _id
from the collections. The operator _id
is required. If you specify a value of null
the $group stage calculates accumulated values for the input document as a whole.
db.collection.aggregate([
{
$group:
{
_id: <expression>,
<field1>: { <accumulator1> : <expression1> },
...
<fieldN>: { <accumulatorN> : <expressionN> },
},
},
]);
The <field>
may be named as you see fit, however, the <accumulator>
must be an accumulator operator.
db.collection.aggregate([
{
$group : {
_id : "$itemId",
count: { $sum: 1},
}
}
]);
The output would be something like this:
{ _id : "Chair", count : 3 }
{ _id : "Table", count : 2 }
{ _id : "Lamp", count : 2 }
- $addToSet
- $avg
- $first
- $last
- $max
- $sum
db.collection.aggregate([
{ $unwind: <field path> },
]);
This operator desconstructs an array field into documents. To be more precisely, a document for each element of the array.
Let's take the following document as an example:
{
_id: 7,
name: "GMusicApp",
subscription: ["free", "premium", "family", "university"]
}
If we apply the $unwind
operator:
db.collection.aggregate([
{ $unwind: "$subscription" },
]);
We would get following output:
{ _id: 7, name: "GMusicApp", subscription: "free" },
{ _id: 7, name: "GMusicApp", subscription: "premium" },
{ _id: 7, name: "GMusicApp", subscription: "family" },
{ _id: 7, name: "GMusicApp", subscription: "university" },
db.collection.aggregate([
{
project: {
<specification(s)>
},
},
]);
Simple speaking, this operator is similar to the 2nd parameter of .find()
. It allows you to manipulate the output, passing along documents with only the specified fields. Those fields can be either from the input documents or newly computed ones.
db.collection.aggregate([
{
$project: {
_id: 0, // or false
productName: "$name",
quantity: 1, // or true
profit: {
$subtract: ["$sale_price", "$cost_price"]
},
},
},
]);
Expression: $abs This arithmetic expression returns the absolute value of a number.
db.collection.aggregate([
{
project: {
delta: {
abs: { $subtract: ["$start", "$end"] }
}
}
}
]);
Expression: $addFields This arithmetic expression let you add new fields to the documents but only into the query output.
db.collection.aggregate([
{
$addFields: {
totalHomework: { $sum: "$homework" } ,
totalQuiz: { $sum: "$quiz" }
}
},
{
$addFields: {
totalScore: {
$add: [ "$totalHomework", "$totalQuiz", "$extraCredit" ]
}
}
}
]);
Expression: $add This arithmetic expression let you sum field values.
db.collection.aggregate([
{ $project: { item: 1, total: { $add: ["$price", "$fee"] } } }
]);
Expression: $subtract This arithmetic expression let you subtract field values.
db.collection.aggregate([
{
$project: {
item: 1,
total: {
$subtract: [
{ $add: ["$price", "$fee"] },
"$discount"
]
}
}
}
]);
Expression: $ceil This arithmetic expression returns the smallest integer that is greater than or equal to the field value.
db.collection.aggregate([
{ $project: { value: 1, ceilingValue: { $ceil: "$value" } } }
]);
Expression: $floor This arithmetic expression returns the smallest integer that is lower than or equal to the field value.
db.collection.aggregate([
{ $project: { value: 1, floorValue: { $floor: "$value" } } }
]);
Expression: $divide This arithmetic expression returns the result of a division between fields.
db.collection.aggregate([
{
project: {
name: 1,
workdays: {
$divide: ["$hours", 8]
}
}
}
]);
Expression: $multiply This arithmetic expression returns the result of a multiplication between fields.
db.collection.aggregate([
{
project: {
date: 1,
item: 1,
total: {
$multiply: ["$price", "$quantity"]
}
}
}
]);