8 and 9
8 and 9
insertMany([ { "studentID": 1, "name": "Student1", "age": 22, "city": "New York", "tags":
["tag1", "tag2", "tag3"] }, { "studentID": 2, "name": "Student2", "age": 25, "city": "San Francisco",
"tags": ["tag1", "tag2", "tag3"] }, { "studentID": 3, "name": "Student3", "age": 30, "city": "Los
Angeles", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 4, "name": "Student4", "age": 35, "city":
"Chicago", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 5, "name": "Student5", "age": 40, "city":
"New York", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 6, "name": "Student6", "age": 45, "city":
"San Francisco", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 7, "name": "Student7", "age": 50,
"city": "Los Angeles", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 8, "name": "Student8", "age":
20, "city": "Chicago", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 9, "name": "Student9", "age":
28, "city": "New York", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 10, "name": "Student10",
"age": 32, "city": "San Francisco", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 11, "name":
"Student11", "age": 26, "city": "Los Angeles", "tags": ["tag2", "tag3"] }, { "studentID": 12, "name":
"Student12", "age": 33, "city": "Chicago", "tags": ["tag1", "tag3"] }, { "studentID": 13, "name":
"Student13", "age": 27, "city": "New York", "tags": ["tag1", "tag2"] }, { "studentID": 14, "name":
"Student14", "age": 31, "city": "San Francisco", "tags": ["tag1", "tag2", "tag3"] }, { "studentID": 15,
"name": "Student15", "age": 36, "city": "Los Angeles", "tags": ["tag2", "tag3"] }, { "studentID": 16,
"name": "Student16", "age": 38, "city": "Chicago", "tags": ["tag1", "tag2"] }, { "studentID": 17,
"name": "Student17", "age": 29, "city": "New York", "tags": ["tag1", "tag3"] }, { "studentID": 18,
"name": "Student18", "age": 34, "city": "San Francisco", "tags": ["tag1", "tag2", "tag3"] },
{ "studentID": 19, "name": "Student19", "age": 23, "city": "Los Angeles", "tags": ["tag1", "tag2"] },
{ "studentID": 20, "name": "Student20", "age": 39, "city": "Chicago", "tags": ["tag1", "tag2", "tag3"] }]
);
1. Unique Index
Let's create a unique index on the `studentID` field to ensure that each studentID is unique across
the collection.
2. Sparse Index
Let's create a sparse index on the `city` field. This means the index will only include documents that
have the `city` field.
The `tags` field is an array, and MongoDB automatically treats an index on an array field as a
multikey index.
4. Compound Index
Let's create a compound index on `city` and `age` to speed up queries that filter by both city and age.
These indexes will help optimize different types of queries on your `students` collection.
Verify Index
db.students.getIndexes();
8B
db.students.find({}).sort({age: -1}).limit(1);
db.students.find({}).sort({age: -1}).limit(1).explain();
db.students.find({}).sort({age: -1}).limit(1).explain("executionStats");
//Now have the total doc scanned after creation of index on age field
db.students.find({}).sort({age:1}).limit(1).explain("executionStats").executionStats.totalDocsExamined;
//now time taken for doc scanned after creation of index on age field
db.students.find({}).sort({age: -1}).limit(1).explain("executionStats").executionStats.executionTimeMillis;
9a .
switched to db kannadaMoviesDB
catalog
kannadaMoviesDB> db.catalog.countDocuments()
701
b. Develop queries to illustrate excluding documents with certain words and phrases
For this we will use the same catalog collection from our previous example. You can follow the same steps as
earlier to create the collection.
Use the $text operator with negation to exclude documents containing specific words or phrases.
The `.toArray()` method is used in MongoDB to convert the results of a query or aggregation operation into a
JavaScript array.
Usage Context
- `find()` Query: When you perform a `find()` operation on a collection, it returns a cursor. To retrieve all
documents from this cursor as an array, we use `.toArray()`.
- `aggregate()` Operation: When you perform an aggregation operation using `aggregate()`, it also returns a
cursor. To get all the aggregated results as an array, you use `.toArray()`.
Detailed Explanation
1. Cursor:
- MongoDB operations like `find()` or `aggregate()` return a cursor. A cursor is an object that allows you to
iterate over the results of the query. It doesn’t load all documents into memory immediately but instead
retrieves documents as needed.
2. `.toArray()`:
- Conversion to Array: The `.toArray()` method retrieves all documents from the cursor and converts them
into a JavaScript array.
- Immediate Execution: When you call `.toArray()`, MongoDB executes the query or aggregation pipeline
immediately and loads all the results into memory as an array.
- Convenience: This method is often used when you want to work with the results as a complete array rather
than iterating through them one by one.
Example
```javascript
result = db.catalog.aggregate([
{ $sort: { avgRating: -1 } },
{ $limit: 5 }
]).toArray();
printjson(result);
What happens here:
- Aggregation Pipeline: The `aggregate()` method runs the aggregation pipeline on the `catalog` collection.
- Cursor Returned: `aggregate()` returns a cursor pointing to the results of the aggregation.
- `toArray()`: The `.toArray()` method is called on this cursor. It retrieves all documents from the cursor and
converts them into a JavaScript array.
- Complete Data Retrieval: It is useful when you need all results immediately and want to work with them as a
complete dataset.
- Debugging and Printing: It allows you to easily print and inspect the results in a readable format.
Performance Consideration
- Memory Usage: Be cautious when using `.toArray()` with large datasets, as it loads all results into memory,
which might not be efficient for very large result sets.
- Stream Processing: For large datasets, consider processing results in a streaming manner or using batch
processing to handle data more efficiently.
What is a Cursor?
A cursor is a server-side object that enables you to retrieve documents from a query result set incrementally.
It acts as an iterator that you can use to access and process documents in a controlled manner.
1. Server-Side Object:
- Cursors are managed on the server side and represent a snapshot of the query results. They do not load all
documents into memory immediately; instead, they fetch documents as needed.
2. Pagination and Iteration:
- Cursors allow you to paginate through large result sets and iterate over documents one by one. This is
useful for handling large datasets without consuming excessive memory.
3. Lazy Evaluation:
- MongoDB uses lazy evaluation for cursors, meaning documents are fetched from the database as they are
requested. This helps optimize memory usage and performance.
4. Stateful:
- Cursors maintain their state between operations, so you can continue processing where you left off if the
result set is large or if you are processing data in chunks.
When you perform a query or aggregation in MongoDB, it returns a cursor. Here’s how you typically work with
a cursor:
1. Query Example:
```javascript
```
You can iterate over the cursor using methods like `.forEach()` or `.next()`, or convert it to an array with
`.toArray()`.
- Using `.forEach()`:
```javascript
cursor.forEach(function(doc) {
printjson(doc);
});
```
- Using `.next()`:
```javascript
while (cursor.hasNext()) {
printjson(cursor.next());
```
- Convert to Array:
```javascript
```
1. `cursor.limit(n)`:
2. `cursor.skip(n)`:
- Sorts the documents by the specified field. Use `1` for ascending order and `-1` for descending order.
4. `cursor.batchSize(n)`:
5. `cursor.explain()`:
Example
Here’s an example demonstrating the use of a cursor with `find()`:
```javascript
cursor.forEach(function(doc) {
printjson(doc);
});
```
In this example:
- `cursor.forEach()` iterates over each document in the cursor and prints it.
Summary
- Definition: A cursor is a server-side object that points to the result set of a MongoDB query or aggregation
operation.
- Functionality: Allows for iterative access to query results, supports pagination, and fetches documents as
needed.
- Usage: Enables efficient handling of large result sets without loading all documents into memory at once.
Cursors are an essential part of working with MongoDB, especially when dealing with large volumes of data or
when need to process documents incrementally.