Professional Documents
Culture Documents
Lecture 3 FULL Explanation
Lecture 3 FULL Explanation
Trouble spots
1
NoSQL
Aggregation
Definition
Example $max
Grouping the documents by the item field, the following operation uses the $max
accumulator to compute the maximum total amount and maximum quantity for each
group of documents.
MongoDB $unwind
The $unwind operator enables us to work with the values of the fields within
an array.
Where there is an array field within the input documents, you will sometimes
need to output the document several times, once for every element of that array.
Each copy of the document has the array field replaced with the successive
element.
Example $out
Backup/restore
1
Basic Backup - mongodump:
1.Install MongoDB Tools: Ensure that MongoDB Tools, which includes
mongodump, are installed on your system. You can download the MongoDB
Tools package from the MongoDB website or install it via package managers
like apt, yum, or brew, depending on your operating system.
2.Run mongodump Command: Open a terminal or command prompt and
execute the mongodump command, specifying the target database and optionally
the host and port if it's not running on the default localhost:27017.
mongodump --host <hostname> --port <port> --db <database_name> --out
<backup_directory>
By following these steps, you can create a JSON Schema with data validation in
MongoDB, ensuring that your documents adhere to predefined rules and constraints,
thus maintaining data integrity and consistency within your database.
NoSQL
Transactions
Transactions
Let's consider a scenario where we need to transfer a student from one course to
another in a university system.
This operation involves updating two collections: students and courses. We want to
ensure that the student is removed from the old course and added to the new course
atomically to maintain data integrity.
Scenario:
• Suppose we have two collections: students and courses.
• Each document in the students collection represents a student, and each
document in the courses collection represents a course.
• Students are associated with courses through a courseId field in the student
document.
Implementing the Transaction:
1. Start a Transaction: Begin by starting a transaction session.
2. Perform Operations within the Transaction:
1. Find the student document and update its courseId to remove the student
from the old course.
2. Find the course document corresponding to the old course and decrement the
enrollmentCount.
3. Find the course document corresponding to the new course and increment
the enrollmentCount.
4. Update the student document to reflect the new course by setting its
courseId.
3. Commit or Abort the Transaction: Depending on the success or failure of the
operations within the transaction, commit or abort the transaction accordingly.
Considerations:
• The transaction ensures that all operations are atomic. If any operation fails, the
transaction will be aborted, and changes will be rolled back.
• By using a transaction, we maintain data integrity, ensuring that the student is
transferred from the old course to the new course atomically.
NoSQL
Geospatial queries
Why we need it
Geospatial queries in MongoDB allow you to perform spatial operations on
geospatial data, such as querying for documents based on their proximity to a point,
within a certain shape, or finding the nearest locations. MongoDB supports two
types of geospatial queries: GeoJSON and Legacy Coordinate Pairs.
Part 1
GeoJSON:
GeoJSON is a format for encoding a variety of geographic data structures.
MongoDB uses GeoJSON objects to represent points, shapes, and other spatial data.
Here's how to use GeoJSON for geospatial queries:
GeoJSON:
c. Performing Geospatial Queries:
Now you can perform various geospatial queries:
Query by Location Proximity:
db.collection.find({ location: {
$near: { $geometry: {
type: "Point",
coordinates: [longitude, latitude]},
$maxDistance: distanceInMeters});
Query Within a Shape:
db.collection.find({
location: {
$geoWithin: {
$geometry: {
type: "Polygon",
coordinates: [/* Define polygon
vertices */]}}}});
Part 1
Geospatial queries in MongoDB allow you to work with spatial data effectively,
enabling applications such as location-based services, geofencing, and spatial
analysis. By understanding the GeoJSON and legacy coordinate pairs formats and
leveraging MongoDB's geospatial indexing capabilities, you can perform a variety of
spatial operations efficiently within your NoSQL database.