-
Notifications
You must be signed in to change notification settings - Fork 0
Querying πΉ
Lyes S edited this page Jun 11, 2022
·
24 revisions
Table Of Contents
// Find All Documents in "recipes" Collection
> db.recipes.find()
// Return Document(s) with an ID (it contains an encoded daytime)
{ "_id" : ObjectId("62a29c6859e7529499943d7c"), "title" : "Tacos", "desc" : "Yummie tacos", "cook_time" : 20 }
// Find All Documents in "recipes" Collection and Display it with Pretty Print
> db.recipes.find().pretty()
// Return Document(s) and Display it Pretty
{
"_id" : ObjectId("62a29c6859e7529499943d7c"),
"title" : "Tacos",
"desc" : "Yummie tacos",
"cook_time" : 20
}
> db.recipes.find({"title" : "Tacos"}).pretty();
{
"_id" : ObjectId("5e5e9c470d33e9e8e3891b35"),
"title" : "Tacos",
"calories_per_serving" : 210,
"cook_time" : 20,
"desc" : "Classic Mexican tacos",
"directions" : [
"Brown beef",
"Add taco seasoning and water, mix",
"Bring to boil",
"Lower heat to simmer 5-10 minutes until desired consistency",
"Put meat in tacos"
],
"ingredients" : [
{
"name" : "ground beef (lean)",
"quantity" : {
"amount" : 1,
"unit" : "lbs"
}
},
{
"name" : "taco seasoning",
"quantity" : {
"amount" : 2,
"unit" : "oz"
}
},
{
"name" : "corn hard tacos",
"quantity" : {
"amount" : 12,
"unit" : "oz"
}
}
],
"likes" : [
1,
415
],
"likes_count" : 2,
"prep_time" : 10,
"rating" : [
4,
4,
3,
4,
2,
5,
2,
2,
4,
5
],
"rating_avg" : 3.5,
"servings" : 4,
"tags" : [
"mexican",
"quick",
"easy",
"ground beef"
],
"type" : "Dinner"
}
- By default, the query result returns all document fields. It is possible to specify the fields you want. To do so, you need to provide a second parameter to find (include attribute = 1).
// Find a document with title "Tacos". The result document should return only "title" attribute.
> db.recipes.find({"title" : "Tacos"}, {"title" : 1}).pretty();
{ "_id" : ObjectId("5e5e9c470d33e9e8e3891b35"), "title" : "Tacos" }
// Find a document with title "Tacos". The result document should return "title" and "type" attributes.
> db.recipes.find({"title" : "Tacos"}, {"title" : 1, "type" : 1}).pretty();
{
"_id" : ObjectId("5e5e9c470d33e9e8e3891b35"),
"title" : "Tacos",
"type" : "Dinner"
}
- By default, the query result returns all document fields. It is possible to specify the fields you want. To do so, you need to provide a second parameter to find (include attribute = 1, exclude attribute = 0).
// Find a document with title "Tacos". The result document should return "title" and "type" attributes and exclude the Id.
> db.recipes.find({"title" : "Tacos"}, {"title" : 1, "type" : 1, "_id" : 0}).pretty();
{ "title" : "Tacos", "type" : "Dinner" }
// Find a document with title based on a regex. The result documents should return "title" attribute.
> db.recipes.find({"title" : {$regex : /taco/i }}, {"title" : 1});
{ "_id" : ObjectId("5e6fd805fa98021236426a24"), "title" : "Chicken Soft Tacos" }
{ "_id" : ObjectId("5e5e9c470d33e9e8e3891b35"), "title" : "Tacos" }
The limit() function in MongoDB is used to specify the maximum number of results to be returned.
> db.recipes.find({ "title" : {$regex : /taco/i} }, {"title" : 1}).limit(1).pretty();
{
"_id" : ObjectId("5e6fd805fa98021236426a24"),
"title" : "Chicken Soft Tacos"
}
Copyright Β© Lyes SEFIANE. All Rights Reserved | CC BY-NC-ND 4.0
