MongoDB provides two methods for finding documents from a collection:
- findOne() – returns a the first document that matched with the specified criteria.
- 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: "john.king@abc.com", salary: 5000 }, { _id:2, firstName: "Sachin", lastName: "T", email: "sachin.t@abc.com", salary: 8000 }, { _id:3, firstName: "James", lastName: "Bond", email: "jamesb@abc.com", salary: 7500 }, { _id:4, firstName: "Steve", lastName: "J", email: "steve.j@abc.com", salary: 9000 }, { _id:5, firstName: "Kapil", lastName: "D", email: "kapil.d@abc.com", salary: 4500 }, { _id:6, firstName: "Amitabh", lastName: "B", email: "amitabh.b@abc.com", salary: 11000 } ])
findOne()
The findOne()
method returns the first document that is matched with the specified criteria.
Syntax:
db.collection.findOne(query, projection)
Parameters:
- query: Optional. Specifies the criteria using query operators.
- 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: findOne()
db.employees.findOne()
Output:
{ _id: 1, firstName: 'John', lastName: 'King', email: 'john.king@abc.com', 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: findOne()
db.employees.findOne({firstName: "Kapil"})
Output:
{ _id: 5, firstName: 'Kapil', lastName: 'D', email: 'kapil.d@abc.com', 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: findOne()
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: findOne()
db.employees.findOne({salary: {$gt: 8000}})
Output:
{ _id: 4, firstName: 'Steve', lastName: 'J', email: 'steve.j@abc.com', 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: findOne()
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: findOne()
db.employees.findOne({firstName: "Sachin"}, {_id: 0, firstName:1, lastName:1})
Output:
{ firstName: 'Sachin', lastName: 'T' }
[…] findOne() – returns a the first document that matched with the specified criteria. […]