Show List

Basic CRUD operations

CRUD stands for Create, Read, Update, and Delete, which are the basic operations used to interact with data in a database. In MongoDB, these operations are performed on documents, which are stored in collections.

Here are some examples of how to perform basic CRUD operations in MongoDB:

  • Create: To create a new document, you can use the insertOne() or insertMany() methods.
php
Copy code
db.users.insertOne({ name: "John", age: 30, email: "john@example.com" })
  • Read: To retrieve data from a collection, you can use the find() method. You can also use various options like limit(), sort(), and skip() to manipulate the returned data.
css
Copy code
db.users.find({name: "John"})
  • Update: To update a document, you can use the updateOne() or updateMany() methods.
css
Copy code
db.users.updateOne( {name: "John"}, {$set: {age: 31}} )
  • Delete: To delete a document, you can use the deleteOne() or deleteMany() methods.
css
Copy code
db.users.deleteOne({name: "John"})

These are just a few examples of the basic CRUD operations in MongoDB. MongoDB also provides many other features and methods for manipulating data, such as aggregation pipelines, transactions, and indexes, making it a powerful tool for managing and querying data.


    Leave a Comment


  • captcha text