Advance Web Application Long Answer WEEK - 2
Advance Web Application Long Answer WEEK - 2
Create (Insert):
To create a new document in MongoDB, you use the insertOne() or insertMany() method.
insertOne() is used to insert a single document into a collection.
insertMany() is used to insert multiple documents into a collection in a single operation.
Example:
To read or retrieve documents from a MongoDB collection, you use the find() method.
find() retrieves documents based on a query/filter and returns a cursor to iterate over the
matched documents.
Example:
Suppose you have a MongoDB database named "mydb" and within that database, you have
two collections: "users" and "orders". The namespaces for these collections would be:
Namespace for the "users" collection: mydb.users
Namespace for the "orders" collection: mydb.orders
Namespaces play a vital role in distinguishing and accessing collections within a database.
They are used in various MongoDB operations, including CRUD operations, indexing,
querying, and administrative tasks. For example, when performing a find() operation, you
would specify the namespace to indicate the collection you want to retrieve data from:
It's important to note that MongoDB has certain naming conventions for namespaces. For
example, namespaces cannot contain certain characters like $ or .. Additionally, the
maximum length of a namespace is 120 bytes.
Understanding namespaces is crucial when working with MongoDB to interact with specific
databases and collections efficiently.
In an Express application, you can handle 404 responses (Not Found) by defining a
middleware that will be executed when no other routes or middleware match the requested
path. Here's how you can handle 404 responses in Express:
Place the middleware at the end of your route and middleware definitions to ensure it is
executed only if no other routes match the requested path.
// Your other routes and middleware definitions
Inside the middleware function, we set the status code to 404 using res.status(404) to
indicate that the requested resource was not found.
We then use res.send() to send the response body, which in this case is a simple text
message "404: Page Not Found". You can customize this message or send an HTML page or
JSON response depending on your application's requirements.
With this setup, whenever a request is made to a route that doesn't exist in your application,
the 404 middleware will be triggered, and the appropriate response will be sent to the
client.
Note: It's important to place the 404 middleware at the end of your middleware chain or
route definitions. If it is placed before any other routes, it will be triggered for all requests,
even if a valid route matches the requested path.
6. get all movies released before the year 2000 or after 2010
const db = client.db(dbName);
Once connected, use the moviesCollection object obtained from the db.collection() method
to perform the queries.
moviesCollection.find().toArray(function(err, documents) {
if (err) {
console.error('Error retrieving documents:', err);
return;
}
console.log('All documents:');
console.log(documents);
});
Get all documents with the writer set to "Quentin Tarantino":