DEV Community

Cover image for CRUD Operations
Saddaul-Siam
Saddaul-Siam

Posted on

CRUD Operations

CRUD Operations

Find Operations

Find a Document

You can query for a single document in a collection with the collection.findOne() method. The findOne() method uses a query document that you provide to match only the subset of the documents in the collection that match the query. If you don't provide a query document or if you provide an empty document, MongoDB matches all documents in the collection. The findOne() operation only returns the first matched document.

const database = client.db("sample_mflix");
const moviesCollection = database.collection("movies");

app.get("/movies", async (req, res) => {
const query = req.query.id;
const result = await moviesCollection.findOne({ _id: ObjectId(query) });
res.json(result);
});

Find Multiple Documents

You can query for multiple documents in a collection with collection.find(). The find() method uses a query document that you provide to match the subset of the documents in the collection that match the query. If you don't provide a query document (or if you provide an empty document), MongoDB returns all documents in the collection.

const database = client.db("sample_mflix");
const moviesCollection = database.collection("movies");

app.get("/movies", async (req, res) => {
const result = await moviesCollection.find({}).toArray();
res.json(result);
});

Insert Operations

Insert a Document

You can insert a document into a collection using the collection.insertOne() method. To insert a document, define an object that contains the fields and values that you want to store. If the specified collection does not exist, the insertOne() method creates the collection.
const database = client.db("sample_mflix");
const moviesCollection = database.collection("movies");

app.post("/movies", async (req, res) => {
const result = await moviesCollection.insertOne(req.body);
res.json(result);
});

Insert Multiple Documents
You can insert multiple documents using the collection.insertMany() method. The insertMany() takes an array of documents to insert into the specified collection.
const database = client.db("sample_mflix");
const namesCollection = database.collection("movies");

app.post("/movies", async (req, res) => {
const name = [
{ name: "siam", jobs: "web developer" },
{ name: "siam", jobs: "web developer" },
{ name: "siam", jobs: "web developer" },
{ name: "siam", jobs: "web developer" },
];
const result = await namesCollection .insertMany(name);
res.json(result);
});

Update Operations
Update a Document

You can update a single document using the collection.updateOne() method. The updateOne() method accepts a filter document and an update document. If the query matches documents in the collection, the method applies the updates from the update document to fields and values of them. The update document contains update operators that instruct the method on the changes to make to the matches.

const database = client.db("sample_mflix");
const moviesCollection = database.collection("movies");

app.put("/movies/:id", async (req, res) => {
const filter = req.params.id;
const options = { upsert: true };
const updateDoc = {
$set: {
name: "saddaul siam",
},
};
const result = await moviesCollection.updateOne(filter, updateDoc, options);
res.json(result);
});

Delete Operations

Delete a Document

You can delete a single document in a collection with collection.deleteOne(). The deleteOne() method uses a query document that you provide to match the subset of the documents in the collection that match the query. If you do not provide a query document (or if you provide an empty document), MongoDB matches all documents in the collection and deletes the first match.

const database = client.db("sample_mflix");
const moviesCollection = database.collection("movies");

app.delete("/movies/:id", async (req, res) => {
const filter = req.params.id;
const result = await moviesCollection.deleteOne(_id:ObjectId(filter));
res.json(result);
});

Top comments (0)