MongoDB read operation findOne() and find()

MongoDB read operation findOne() and find()

MongoDB 05-02-2023 Saheb Sutradhar

MongoDB read operation findOne() and find()

 

Read operation in mongoDB is reading or fetching data from mongoDB database , for this we have to methods .

 

findOne()

 

db.collectionName.findOne();
db.collectionName.findOne({name:'john'});

findOne() method without any filter returns the first document from the collection.

findOne({name:'john'})  returns the first matched document from the collection where name is john.

Data

[
    {
        name:'alex',
        hobby:[
            {
                title:'sports',
                active:2
            },
            {
                title:'coding',
                active:3
            }
        ]
    },
    {
        name:'john',
        hobby:[
            {
                title:'sports',
                active:2
            },
            {
                title:'work',
                active:3
            }
        ]
    }
]

 

Query

db.collectionName.findOne({name:'john'})

Output

 {
        name:'john',
        hobby:[
            {
                title:'sports',
                active:2
            },
            {
                title:'work',
                active:3
            }
        ]
    }

 

 

find()

db.collectionName.find()
db.collectionName.find({place:'bangalore'})

find() returns the all the document from the collection , it returns a cursor , means if you have 2000 or more document in the collection , find() method will not return all the document it will return 20 in the shell.

find({place:'bangalore'}) find() with filter returns all the documents from the collection after filtering it.

 

Data

[
    {
        name:'alex',
        place:'bangalore',
        hobby:[
            {
                title:'sports',
                active:2
            },
            {
                title:'coding',
                active:3
            }
        ]
    },
    {
        name:'john',
        place:'bangalore',
        hobby:[
            {
                title:'sports',
                active:2
            },
            {
                title:'work',
                active:3
            }
        ]
    }
]

Query

db.collectionName.find({place:'bangalore'})

Output

[
    {
        name:'alex',
        place:'bangalore',
        hobby:[
            {
                title:'sports',
                active:2
            },
            {
                title:'coding',
                active:3
            }
        ]
    },
    {
        name:'john',
        place:'bangalore',
        hobby:[
            {
                title:'sports',
                active:2
            },
            {
                title:'work',
                active:3
            }
        ]
    }
]

 

 

 

 

 

 

 

 

 

 

Related Posts

Card image cap

MongoDB Basic and CRUD Operations

MongoDB 02-09-2022 Saheb Sutradhar

MongoDB Basic and CRUD Operations ...

Card image cap

MongoDB Read Array Document

MongoDB 05-02-2023 Saheb Sutradhar

MongoDB Read Array Document ...

Card image cap

MongoDB upsert

MongoDB 02-03-2023 Saheb Sutradhar

MongoDB upsert ...

Card image cap

MongoDB $unset

MongoDB 02-03-2023 Saheb Sutradhar

MongoDB $unset ...

Card image cap

MongoDB $rename

MongoDB 02-03-2023 Saheb Sutradhar

MongoDB $rename ...

Card image cap

MongoDB array update operation

MongoDB 13-03-2023 Saheb Sutradhar

MongoDB array update operation ...