Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • MongoDB - Get Started
  • What is MongoDB?
  • Install MongoDB
  • MongoDB Server
  • MongoDB Shell
  • MongoDB Shell Commands
  • MongoDB Compass
  • MongoDB Database
  • MongoDB Collections
  • MongoDB Documents
  • Insert Single Document
  • Insert Multiple Documents
  • Import Data into Collection
  • Find Single Document
  • Find Multiple Documents
  • MongoDB Cursor
  • MongoDB Sort Documents
  • Update Single Document
  • Update Multiple Documents
  • Update Arrays
  • Update Embedded Documents
  • Delete Documents
  • Relations in MongoDB
  • Aggregation in MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

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' }
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.