Learning MongoDB

0% completed

Previous
Next
Read/Query Documents

Read/query operations in MongoDB are essential for retrieving data from a collection. These operations allow you to find and manipulate documents based on specified criteria. MongoDB provides several methods for querying documents, each with its own set of features and use cases.

This lesson will cover the following methods:

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

find() Method

The find() method is used to query documents in a collection that match the specified criteria. It returns a cursor to the documents that match the query. This method is highly flexible and allows for extensive querying capabilities, including filtering, projection, sorting, and pagination.

Syntax

db.collection.find( <query>, <projection> )
  • query (optional): Specifies the criteria for the query.
  • projection (optional): Specifies the fields to return in the documents.

Examples

  1. Basic Query

    db.users.find({ age: { $gte: 30 } })

    This command retrieves all documents where the age field is greater than or equal to 30.

  2. Query with Projection

    db.users.find({ age: { $gte: 30 } }, { name: 1, age: 1, _id: 0 })

    This command retrieves all documents where the age field is greater than or equal to 30 and returns only the name and age fields, excluding the _id field.

findOne() Method

The findOne() method is used to query a single document that matches the specified criteria. It returns the first document that matches the query.

Syntax

db.collection.findOne( <query>, <projection> )
  • query (optional): Specifies the criteria for the query.
  • projection (optional): Specifies the fields to return in the document.

Examples

  1. Basic Query

    db.users.findOne({ name: "Alice" })

    This command retrieves the first document where the name is "Alice".

  2. Query with Projection

    db.users.findOne({ name: "Alice" }, { name: 1, age: 1, _id: 0 })

    This command retrieves the first document where the name is "Alice" and returns only the name and age fields, excluding the _id field.

findAndModify() Method

The findAndModify() method is used to modify and return a single document. It atomically modifies the document and returns it.

Syntax

db.collection.findAndModify({ query: <document>, sort: <document>, remove: <boolean>, update: <document>, new: <boolean>, fields: <document>, upsert: <boolean> })
  • query: Specifies the criteria for selecting the document.
  • sort (optional): Determines which document to modify if multiple documents match the query.
  • remove (optional): If true, removes the selected document.
  • update (optional): Specifies the modifications to apply.
  • new (optional): If true, returns the modified document rather than the original.
  • fields (optional): Specifies the fields to return.
  • upsert (optional): If true, inserts a new document if no document matches the query.

Examples

  1. Modify and Return Document

    db.users.findAndModify({ query: { name: "Bob" }, update: { $set: { age: 31 } }, new: true })

    This command modifies the first document where the name is "Bob", sets the age to 31, and returns the modified document.

  2. Remove and Return Document

    db.users.findAndModify({ query: { name: "Charlie" }, remove: true })

    This command removes the first document where the name is "Charlie" and returns the removed document.

findOneAndDelete() Method

The findOneAndDelete() method is used to delete a single document that matches the specified criteria and return the deleted document.

Syntax

db.collection.findOneAndDelete( <filter>, <options> )
  • filter: Specifies the criteria for the query.
  • options (optional): Specifies options for the delete operation.

Examples

  1. Delete and Return Document

    db.users.findOneAndDelete({ name: "Alice" })

    This command deletes the first document where the name is "Alice" and returns the deleted document.

findOneAndUpdate() Method

The findOneAndUpdate() method is used to update a single document that matches the specified criteria and return the updated document.

Syntax

db.collection.findOneAndUpdate( <filter>, <update>, <options> )
  • filter: Specifies the criteria for the query.
  • update: Specifies the modifications to apply.
  • options (optional): Specifies options for the update operation.

Examples

  1. Update and Return Document

    db.users.findOneAndUpdate( { name: "Bob" }, { $set: { age: 32 } }, { returnNewDocument: true } )

    This command updates the first document where the name is "Bob", sets the age to 32, and returns the updated document.

findOneAndReplace() Method

The findOneAndReplace() method is used to replace a single document that matches the specified criteria with a new document and return the replaced document.

Syntax

db.collection.findOneAndReplace( <filter>, <replacement>, <options> )
  • filter: Specifies the criteria for the query.
  • replacement: The new document that will replace the existing document.
  • options (optional): Specifies options for the replace operation.

Examples

  1. Replace and Return Document

    db.users.findOneAndReplace( { name: "Charlie" }, { name: "Charlie", age: 36, email: "charlie.new@example.com" }, { returnNewDocument: true } )

    This command replaces the first document where the name is "Charlie" with a new document and returns the new document.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next