Week 2 - Advance Web Application Dev
Week 2 - Advance Web Application Dev
Ans: - CRUD stands for Create, Read, Update, and Delete. These are the four basic operations that
are performed on data in most database systems. MongoDB, a popular NoSQL database, supports
these CRUD operations as well. Below is a detailed explanation of each CRUD operation in the
context of MongoDB:
1. Create (C)
In MongoDB, the insertOne() and insertMany() methods are used to create new documents in a
collection.
insertOne()
db.collection('users').insertOne({
email: 'john.doe@example.com'
});
insertMany()
db.collection('users').insertMany([
age: 25,
email: 'jane.smith@example.com'
},
age: 35,
email: 'alice.johnson@example.com'
}
]);
2. Read (R)
find()
The find() method retrieves documents from a collection. You can also use query criteria to filter the
documents.
db.collection('users').find().toArray();
3. Update (U)
In MongoDB, the updateOne() and updateMany() methods are used to update documents in a
collection.
updateOne()
The updateOne() method updates a single document that matches the filter criteria.
db.collection('users').updateOne(
{ $set: { age: 32 } }
);
updateMany()
The updateMany() method updates all documents that match the filter criteria.
db.collection('users').updateMany(
{ age: { $gt: 30 } },
{ $set: { age: 40 } }
);
4. Delete (D)
In MongoDB, the deleteOne() and deleteMany() methods are used to delete documents from a
collection.
deleteOne()
The deleteOne() method deletes a single document that matches the filter criteria.
// Delete the document where name is 'Alice Johnson'
deleteMany()
The deleteMany() method deletes all documents that match the filter criteria.
Create (C)
Read (R)
find(): Retrieve documents from a collection.
Update (U)
Delete (D)
Ans: - In MongoDB, a namespace refers to the combination of a database name and a collection
name. It uniquely identifies a collection within a database. The namespace is used to perform CRUD
operations on a specific collection within a database.
Format of a Namespace
The format of a namespace in MongoDB is:
<database_name>.<collection_name>
Examples
myDatabase.users
eCommerce.products
Importance of Namespace
Performing CRUD Operations: When performing CRUD operations, you need to specify the
namespace to indicate on which collection the operation should be performed.
When performing CRUD operations in MongoDB, you specify the namespace to target a specific
collection.
Example:
To insert a document into a collection named users in a database named myDatabase, you would
use the following namespace:
db.myDatabase.users.insertOne({
age: 30,
email: 'john.doe@example.com'
});
Here, db.myDatabase.users is the namespace for the users collection in the myDatabase database.
Ans: - Handling 404 responses is crucial in web development to gracefully manage situations where
a requested resource is not found. In a MongoDB context, 404 errors often occur when trying to
access or manipulate data that does not exist in the database. Here are some ways to handle 404
responses in a MongoDB application:
If you are using Express.js with Node.js, you can handle 404 responses using the app.use()
middleware at the end of all other routes.
// Other routes
if (err) {
if (!user) {
res.json(user);
});
});
});
app.listen(3000, () => {
});
You can also create a custom error-handling middleware to handle 404 responses.
// Other routes
if (err) {
if (!user) {
error.status = 404;
res.json(user);
});
});
});
app.listen(3000, () => {
});
Using async/await with try/catch blocks is another approach to handle 404 responses.
const express = require('express');
try {
if (!user) {
res.json(user);
} catch (err) {
});
app.listen(3000, () => {
});
4) Connect to a running mongo instance, use a database named mongo_practice.
Document all your queries.
Insert the documents into a movies collection.
query the movies collection to
6. get all movies released before the year 2000 or after 2010.
Ans: - To perform the mentioned tasks, you can use the MongoDB shell or a MongoDB client.
Below are the queries to connect to a running MongoDB instance, use a database named
mongo_practice, and insert documents into a movies collection. I will also include the queries to
perform the requested operations on the movies collection.
Step 1: Connect to a running MongoDB instance and use the mongo_practice database
Open the MongoDB shell or a MongoDB client and execute the following commands to connect
to the running MongoDB instance and switch to the mongo_practice database.
use mongo_practice
Execute the following command to insert documents into the movies collection.
db.movies.insertMany([
releaseYear: 1994
},
franchise: "No",
releaseYear: 2009
},
releaseYear: 2012
},
releaseYear: 2013
},
releaseYear: 1999
]);
db.movies.find().pretty();
6) Get all movies released before the year 2000 or after 2010