db.collection.find(query, projected)
1. find all the result of the given collection.
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
shreyashdb 0.000GB
> use shreyashdb
switched to db shreyashdb
> show collections
shreyashdb
> db.shreyashdb.find()
{ "_id" : ObjectId("61ca837682bfb31fe50836eb"), "name" : "ReactJS", "type" : "Front End", "blogpost" : 100, "active" : true }
{ "_id" : ObjectId("61ca89a182bfb31fe50836ec"), "name" : "shreyash kolhe", "email" : "shreyashkolhe2001@gmail.com", "role" : "developer", "active" : true }
{ "_id" : ObjectId("61ca8c9282bfb31fe50836ed") }
{ "_id" : ObjectId("61ca8c9282bfb31fe50836ee") }
{ "_id" : ObjectId("61ca8c9282bfb31fe50836ef") }
{ "_id" : ObjectId("61ca8c9282bfb31fe50836f0") }
>
2.show the result in pretty format?
> db.shreyashdb.find().pretty()
{
"_id" : ObjectId("61ca837682bfb31fe50836eb"),
"name" : "ReactJS",
"type" : "Front End",
"blogpost" : 100,
"active" : true
}
{
"_id" : ObjectId("61ca89a182bfb31fe50836ec"),
"name" : "shreyash kolhe",
"email" : "shreyashkolhe2001@gmail.com",
"role" : "developer",
"active" : true
}
{ "_id" : ObjectId("61ca8c9282bfb31fe50836ed") }
{ "_id" : ObjectId("61ca8c9282bfb31fe50836ee") }
{ "_id" : ObjectId("61ca8c9282bfb31fe50836ef") }
{ "_id" : ObjectId("61ca8c9282bfb31fe50836f0") }
>
3. Get only MongoDB data as a output.
> db.shreyashdb.find({name:"shreyash kolhe"})
{ "_id" : ObjectId("61ca89a182bfb31fe50836ec"), "name" : "shreyash kolhe", "email" : "shreyashkolhe2001@gmail.com", "role" : "developer", "active" : true }
> db.shreyashdb.find({name:"shreyash kolhe"}).pretty()
{
"_id" : ObjectId("61ca89a182bfb31fe50836ec"),
"name" : "shreyash kolhe",
"email" : "shreyashkolhe2001@gmail.com",
"role" : "developer",
"active" : true
}
4. Get only MongoDB data as a output with only name field.
> db.shreyashdb.find({name:"shreyash kolhe"},{name:1}).pretty()
{ "_id" : ObjectId("61ca89a182bfb31fe50836ec"), "name" : "shreyash kolhe" }
> db.shreyashdb.find({name:"shreyash kolhe"},{name:0}).pretty()
{
"_id" : ObjectId("61ca89a182bfb31fe50836ec"),
"email" : "shreyashkolhe2001@gmail.com",
"role" : "developer",
"active" : true
}
>
5. Get the MongodB data without _ID field in it.
> db.shreyashdb.find({name:"shreyash kolhe"},{_id:0, name:1}).pretty()
{ "name" : "shreyash kolhe" }
>
6. set the filter to 'active:true' and get only the first field with "active:true" value.
> db.shreyashdb.find({active:true}).pretty()
{
"_id" : ObjectId("61ca837682bfb31fe50836eb"),
"name" : "ReactJS",
"type" : "Front End",
"blogpost" : 100,
"active" : true
}
{
"_id" : ObjectId("61ca89a182bfb31fe50836ec"),
"name" : "shreyash kolhe",
"email" : "shreyashkolhe2001@gmail.com",
"role" : "developer",
"active" : true
}
> db.shreyashdb.find({active:true}).pretty().limit(1)
{
"_id" : ObjectId("61ca837682bfb31fe50836eb"),
"name" : "ReactJS",
"type" : "Front End",
"blogpost" : 100,
"active" : true
}
>
7. Do the same as 6 questions but with different methods
> db.shreyashdb.findOne({active:true})
{
"_id" : ObjectId("61ca837682bfb31fe50836eb"),
"name" : "ReactJS",
"type" : "Front End",
"blogpost" : 100,
"active" : true
}
>
8. Do the same as 5 questions but this time, get the 2nd field with active:true by skipping the 1st field.
> db.shreyashdb.find({active:true}).pretty().limit(1).skip(1)
{
"_id" : ObjectId("61ca89a182bfb31fe50836ec"),
"name" : "shreyash kolhe",
"email" : "shreyashkolhe2001@gmail.com",
"role" : "developer",
"active" : true
}
>
Post a Comment