Previous lessons
Table of contents
Download and import restaurants collection
- Open mongodb compass
Connect to your local host
Create new database called shops
- Click Add Data
- Import JSON file
Select the downloaded JSON from my github repository
Open mongosh terminal
Use shops database.
use shops
Read Queries
$eq Stands for Equal
$eq is an operator used to match documents where the value of a field is equal to a specified value.
find documents with rating equal to 4.
db.restaurants.find(
{
rating:{
$eq: 4
}
}
)
//or you can just match directly without using $eq
db.restaurants.find(
{
rating: 4
}
)
$elemMatch vs matching directly
db.restaurants.find(
{
"grades": {
$elemMatch:{
grade: "A",
score:6
grade: "A"
}
}
}
)
db.restaurants.find(
{
"grades.grade": "A"
"grades.score": 6
}
)
matching directly like This means that the "grades" array may contain other elements besides the one with "grade" equal to "A" so it will return this document.
$elemMatch will make sure that the element inside grades array must have one element having score of 6 and grade of A so it will not return this document.
$ne
$ne stands for Not Equal.
find restaurants with grades not equal to "A".
db.restaurants.find(
{
grades: {
$elemMatch: {
grade: {
$ne: "A"
}
}
}
}
)
$gt
$gt Stands For Greater Than.
find restaurants with rating greater than 3.
db.restaurants.find({ rating: { $gt: 3} })
$gte
$gte Stands For Greater Than.
find restaurants with rating greater than or equal 4.
db.restaurants.find({ rating: { $gte: 4} })
$lt
Stands for Less Than.
find restaurants with rating lower than 2.
db.restaurants.find({ shopRank: { $lt: 2} })
$lte
Stands for Less Than Or Equal.
find restaurants with rating lower than or equal 5.
db.restaurants.find({ rating: { $lte: 5} })
$in
Find restaurants in these three cuisine categories "Bakery", "Irish", "American" 1, 3, 5.
db.restaurants.find(
{
cuisine: {
$in: ["American", "Irish", "Bakery"]
}
}
)
$nin
$nin stands for Not In.
Find restaurants not in these three shop ranks 1, 3, 5 .
db.restaurants.find(
{
cuisine: {
$nin: ["American", "Irish", "Bakery"]
}
}
)
$and
$and is a logical operator used within queries to specify multiple conditions
$and returns all documents that meets all conditions specified
Find restaurants with with cuisine equal to "American" and rating higher than 3.
db.restaurants.find(
{
$and: [
{ cuisine: "Irish" },
{ rating: { $gt: 1 } }
]
}
)
$or
$or is another logical operator used within queries to specify multiple conditions but unlike $and $or requires at least one condition to be met
Find restaurants with with cuisine equal to "American" or rating higher than 3.
db.restaurants.find(
{
$or: [
{ cuisine: "Irish" },
{ rating: { $gt: 1 } }
]
}
)
$nor
$nor is the exact opposite or $or it finds documents that doesn't meet any of the conditions .
Find restaurants with with cuisine not equal to "American" or rating not higher than 3.
db.restaurants.find(
{
$or: [
{ cuisine: "Irish" },
{ rating: { $gt: 1 } }
]
}
)
$not
$not is the exact opposite or $eq.
Find restaurants with with cuisine not equal to "American" or rating not higher than 3.
db.restaurants.find(
{
cuisine: {
$not: { $eq: "American" }
}
}
)
$regex
$regex operator is used to perform regular expression-based pattern matching within queries. It allows you to search for documents where a specified field matches a regular expression pattern.
Find restaurants where the name starts with mor and case insensitive.
db.restaurants.find(
{
title: {
$regex: /mor/,
$options: "i" // case insensitive option
}
}
)
$text and $search
$text and $search operator is used to perform a full-text search on text fields within a collection. It's specifically designed for searching text content efficiently.
Find restaurants where the name starts with morris.
db.restaurants.find(
{
$text: { $search: "morris" }
}
)
$where
$where allows you to write javascript code directly into the query.
Find restaurants where the rating divided by two is still higher that 1.5 .
db.restaurants.find(
{
$where: function () {
return this.rating / 2 > 1.5;
}
}
)
Top comments (2)
thx
Hope you liked it