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()
orinsertMany()
methods.
phpCopy 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 likelimit()
,sort()
, andskip()
to manipulate the returned data.
cssCopy code
db.users.find({name: "John"})
- Update: To update a document, you can use the
updateOne()
orupdateMany()
methods.
cssCopy code
db.users.updateOne(
{name: "John"},
{$set: {age: 31}}
)
- Delete: To delete a document, you can use the
deleteOne()
ordeleteMany()
methods.
cssCopy 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