MongoDB Find and FindOne Collection and Shell Method

Mongo Db Collection Method


We will perform all the mongoDB Shell collection methods on studentInfo DB. Create studentInfo Database in MongoDB server. Use the below code for creating the DB.

 

Using db.createCollection() method, create collection in database. Command to create studnet collection in studentInfo Database is below 


Insert Some Document in student collection where we can apply the MongoDB Collection Methods to explore further usage. I am Inserting five document in Student Collection.
Mongo Collection methods and example

Mongo Findone Shell Collection Method


Above Image contain the document in student collection. We will use student Collection from studentInfo DB to execute our Find One Mongo Methods. We will use pretty() function at the end of all the collection to get the output in parse format.

  • db.collectionName. find()
  • db.collectionName. findOne()
  • db.collectionName. findAndModify()
  • db.collectionName. findOneAndDelete()
  • db.collectionName. findOneAndReplace()
  • db.collectionName. findOneAndUpdate()

 db.student.find().pretty() :-
This above MongoDB Shell find Query will return all the document present in the Student Collection without filtering anything.

{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf55"),
        "id" : 2,
        "name" : "MOHAMMED ISHAQ  H",
        "dob" : "2002-06-14",
        "marks" : 67,
        "created_at" : "2016-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf56"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"),
        "id" : 4,
        "name" : "MARTINPRIYADOSS J",
        "dob" : "2004-03-08",
        "marks" : 43,
        "created_at" : "2012-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebf58376803d36568082c60"),
        "id" : 5,
        "name" : "JAGAN J",
        "dob" : "2003-04-10",
        "marks" : 98,
        "created_at" : "2012-11-13 23:33:42"
}

db.student.findOneAndDelete({name:"JAGAN J"})
This above query will find the document in which document.name will be equal to "JAGAN J" and it will delete that particular record and return the deleted document.
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf58"),
        "id" : 5,
        "name" : "JAGAN J",
        "dob" : "2003-04-10",
        "marks" : 98,
        "created_at" : "2012-11-13 23:33:42"
}

db.student.findOne()
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}
db.collectionName.findOne() method will return the first matching record from the document collection.

db.student.find({name:"M Kumar"}).forEach(printjson)
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "id" : 1,
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54,
        "created_at" : "2017-11-13 23:33:42"
}

db.student.find({name:"M Kumar"},{name:1,dob:1,marks:1}).pretty()
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf54"),
        "name" : "M Kumar",
        "dob" : "2003-04-07",
        "marks" : 54
}

Note :- Find() shell Collection Method will pull all the collection document fields. If you want some certain collection field then you have to pass the field-name : 1 as the second parameter. Example code is above for reference.

Logical Operator and Filter on FindOne Shell Method


db.student.findOne({marks :{$lt:50}},{name:1,dob:1})
{
        "_id" : ObjectId("5ebeaafd50845b5c0ea8bf57"),
        "name" : "MARTINPRIYADOSS J",
        "dob" : "2004-03-08"
}

db.collection.find({$or:[{marks :{$gt:70}},{name:"JAGAN J"}]}).pretty()
{
        "_id" : ObjectId("5ebea4f850845b5c0ea8bf51"),
        "id" : 3,
        "name" : "ANBUSELVAM S",
        "dob" : "2004-06-18",
        "marks" : 87,
        "created_at" : "2019-11-13 23:33:42"
}
{
        "_id" : ObjectId("5ebea4f850845b5c0ea8bf53"),
        "id" : 5,
        "name" : "JAGAN J",
        "dob" : "2003-04-10",
        "marks" : 98,
        "created_at" : "2012-11-13 23:33:42"
}

No comments:

Post a Comment

Our Feature Post

There is a tree between houses of A and B If the tree leans on As House

    There is a tree between houses of A and B. If the tree There is a tree between houses of A and B. If the tree leans on A’s House, the t...

Our Popular Post