Tutorialsteacher

Follow Us

MongoDB: Find Single Document from Collection using findOne()

In MongoDB, a collection represents a table in RDBMS and a document is like a record in a table. Here you will learn how to retrieve or find a single document from a collection.

MongoDB provides two methods for finding documents from a collection:

  1. findOne() - returns a the first document that matched with the specified criteria.
  2. find() - returns a cursor to the selected documents that matched with the specified criteria.

The following inserts documents in the employees collection.

Sample Data
db.employees.insertMany([
    { 
        _id:1,
        firstName: "John",
        lastName: "King",
        email: "[email protected]",
        salary: 5000
    },
    { 
        _id:2,
        firstName: "Sachin",
        lastName: "T",
        email: "[email protected]",
        salary: 8000
    },
    { 
        _id:3,
        firstName: "James",
        lastName: "Bond",
        email: "[email protected]",
        salary: 7500
    },
    { 
        _id:4,
        firstName: "Steve",
        lastName: "J",
        email: "[email protected]",
        salary: 9000

    },
    { 
        _id:5,
        firstName: "Kapil",
        lastName: "D",
        email: "[email protected]",
        salary: 4500

    },
    { 
        _id:6,
        firstName: "Amitabh",
        lastName: "B",
        email: "[email protected]",
        salary: 11000
    }
])

findOne()

The findOne() method returns the first document that is matched with the specified criteria.

Syntax:

db.collection.findOne(query, projection)

Parameters:

  1. query: Optional. Specifies the criteria using query operators.
  2. projection: Optional. Specifies the fields to include in a resulted document.

The findOne() returns the first document from a collection if no parameter is passed.

Example: find()
db.employees.findOne()
Output
{
  _id: 1,
  firstName: 'John',
  lastName: 'King',
  email: '[email protected]',
  salary: 5000
}

Specify a criteria as { field: "value", field:"value",..} on which you want to find a document. For example, the following returns a document where firstName field is "Kapil".

Example: find()
db.employees.findOne({firstName: "Kapil"})
Output
{
  _id: 5,
  firstName: 'Kapil',
  lastName: 'D',
  email: '[email protected]',
  salary: 4500
}

The findOne() method performs the case-sensitive search, so {firstName: "Kapil"} and {firstName: "kapil"} returns different result.

It returns null if it cannot find a document matching the specified criteria.

Example: find()
db.employees.findOne({_id:10})
Output
null

Use the query operators for more refined search. For example, the following finds the first document where salary is greater than 8000.

Example: find()
db.employees.findOne({salary: {$gt: 8000}})
Output
{
  _id: 4,
  firstName: 'Steve',
  lastName: 'J',
  email: '[email protected]',
  salary: 9000
}

In the above example, the query operator criteria for the salary field is written inside another document as {field: {operator: value}}. The {salary: {$gt: 8000}} criteria returns the first document where salary is greater than 8000.

Projection

Use the projection parameter to specify the fields to be included in the result. The projection parameter format is {<field>: <1 or true>, <field>: <1 or true>...} where 1 or true includes the field, and o or false excludes the field in the result.

Example: find()
db.employees.findOne({firstName: "Sachin"}, {firstName:1, lastName:1})
Output
{ _id: 2, firstName: 'Sachin', lastName: 'T' }

Note that by default, the _id field will be included in the result. To omit it, specify { _id:0 } in the projection.

Example: find()
db.employees.findOne({firstName: "Sachin"}, {_id: 0, firstName:1, lastName:1})
Output
{ firstName: 'Sachin', lastName: 'T' }