MongoDB CRUD Operations

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:03:00

What are MongoDB's CRUD operations?

CRUD Operations (Create, Read, Update, and Delete) are the fundamental set of operations that enable users to interact with the MongoDB server.

Create Operations

The create or insert operations are used to add new documents to the collection. If a collection does not already exist in the database, it will be created.

  • db.collection.insert(<document(s)>): This method inserts one or more documents into a collection. It takes a single document or an array of documents as input.
  • db.collection.insertOne(<document>): db.collection.insertOne(<document>) inserts a single document into the collection. It takes a single document as an argument.
  • db.collection.insertMany(<documents>): db.collection.insertMany(<documents>) inserts several documents into a collection. It takes an array of documents as an argument.

Read Operations

Read operations are used for getting documents from a collection, or to search for a document inside a collection.

  • db.collection.find(<filter>): This function retrieves documents from a collection that meets the given filter requirements. It takes a query filter as its parameter.
  • db.collection.findOne(<filter>): db.collection.findOne(<filter>) retrieves a single document from a collection that meets the given filter requirements. It takes a query filter as its parameter.
  • pretty(): Pretty() is a method that displays query results in a structured JSON structure, making them easier to read.

Update Operations

The update operations are used to update or change an existing document in the collection. MongoDB provides the following ways for performing update operations:

  • db.collection.update(<filter>,<document>): This method updates one or more documents in a collection that match the provided filter criteria. 
  • db.collection.updateOne(<filter>,<document>): This method updates a single document in a collection that satisfies the provided filter requirements. 
  • db.collection.updateMany(<filter>,<document>): This method updates numerous documents in a collection that meets the provided filter parameters. 

Delete Operations

The delete operation is used to delete or remove documents from a collection. MongoDB provides the following ways to perform delete operations:

  • db.collection.remove(<filter>): The remove() method removes one or more documents from a collection that meet the specified filter criteria.
  • db.collection.deleteOne(<filter>): This function removes a single document from a collection that meets the offered filter requirements.
  • db.collection.deleteMany(<filter>): This function removes numerous documents from a collection that meet the provided filter conditions. 

Find Document(s) and Perform Operations on MongoDB

These MongoDB operations involve locating a document based on offered filter criteria and performing a certain operation on it. MongoDB has multiple techniques for performing these operations:

  • db.collection.findOneAndDelete(<filter>): This method finds and deletes a single document in a collection that meets the offered filter criteria, and returns the deleted document.
  • db.collection.findOneAndUpdate(<filter>, <update>): This method locates and updates a single document in a collection that matches the provided filter criteria. It returns either the original or updated document.
  • db.collection.findOneAndReplace(<filter>, <replacement>): This method finds a single document in a collection that meets the filter criteria, replaces it with the provided replacement document, and returns either the original or the replaced document.
  • db.collection.findAndModify(<filter>,<document>): The findAndModify() method is similar to findOneAndUpdate(), however it is deprecated in newer MongoDB versions and replaced by findOneAndUpdate(). It locates a single document in a collection that meets the provided filter criteria, modifies it based on the specified document, & returns either the original or the modified document.
Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 50+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this