0% found this document useful (0 votes)
8 views12 pages

8 and 9

The document outlines various MongoDB operations, including inserting student records and creating different types of indexes to optimize query performance. It discusses unique, sparse, multikey, and compound indexes, as well as the use of cursors for efficient data retrieval. Additionally, it explains the use of the `.toArray()` method and provides examples of querying and processing data in MongoDB.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

8 and 9

The document outlines various MongoDB operations, including inserting student records and creating different types of indexes to optimize query performance. It discusses unique, sparse, multikey, and compound indexes, as well as the use of cursors for efficient data retrieval. Additionally, it explains the use of the `.toArray()` method and provides examples of querying and processing data in MongoDB.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

db.students.

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.

db.students.createIndex({ studentID: 1 }, { unique: true });

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.

db.students.createIndex({ city: 1 }, { sparse: true });


3. Multikey Index

The `tags` field is an array, and MongoDB automatically treats an index on an array field as a
multikey index.

db.students.createIndex({ tags: 1 });

4. Compound Index

Let's create a compound index on `city` and `age` to speed up queries that filter by both city and age.

db.students.createIndex({ city: 1, age: 1 });

These indexes will help optimize different types of queries on your `students` collection.

Verify Index

db.students.getIndexes();

8B

FIND THE SLOW QUERY WITHOUT CREATING ANY INDEX

// Find the student with the highest age

db.students.find({}).sort({age: -1}).limit(1);

// Explain the query plan

db.students.find({}).sort({age: -1}).limit(1).explain();

// Explain the query with execution statistics

db.students.find({}).sort({age: -1}).limit(1).explain("executionStats");

//Now have the total doc scanned


db.students.find({}).sort({age:1}).limit(1).explain("executionStats").executionStats.totalDocsExamined;

//now time taken for doc scanned


db.students.find({}).sort({age: -1}).limit(1).explain("executionStats").executionStats.executionTimeMillis;

NOW CREATE AN INDEX ON age Field

db.students.createIndex({ age: 1 });

Repeat the 2 commands like

//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 .

test> use kannadaMoviesDB

switched to db kannadaMoviesDB

kannadaMoviesDB> show collections

catalog

kannadaMoviesDB> db.catalog.countDocuments()

701

Step 2: Create a Text Index

Step 3: Perform a Text Search Query


To find “raju”
Step 4: Perform a Text Search Query for a phrase

b. Develop queries to illustrate excluding documents with certain words and phrases

Step 1: Create a catalog collection

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.

Step 2: Perform Queries to Exclude Documents

Use the $text operator with negation to exclude documents containing specific words or phrases.

Example 1: Exclude Documents Containing the Word “action”

Example 2: Exclude Documents Containing the Phrase “da maga”


10

The `.toArray()` method is used in MongoDB to convert the results of a query or aggregation operation into a
JavaScript array.

Usage Context

In the context of MongoDB queries or aggregation pipelines:

- `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

Let’s use an example to illustrate:

```javascript

result = db.catalog.aggregate([

{ $match: { year: 2017 } },

{ $group: { _id: "$genre", avgRating: { $avg: "$rating" } } },

{ $sort: { avgRating: -1 } },

{ $limit: 5 }

]).toArray();

print("Top 5 rated movie genres with their average rating");

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.

- `result`: The variable `result` now holds this array of documents.

Why Use `.toArray()`?

- 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.

MORE UNDERSTANDING ON CURSOR

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.

Key Characteristics of a Cursor

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.

How to Use a Cursor

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

var cursor = db.collection.find({ field: "value" });

```

2. Iterate Over Documents:

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

var docs = cursor.toArray();

```

Cursor Methods and Options

1. `cursor.limit(n)`:

- Limits the number of documents returned by the cursor to `n`.

2. `cursor.skip(n)`:

- Skips the first `n` documents of the result set.

3. `cursor.sort({ field: 1 })`:

- Sorts the documents by the specified field. Use `1` for ascending order and `-1` for descending order.

4. `cursor.batchSize(n)`:

- Specifies the number of documents to return in each batch of the cursor.

5. `cursor.explain()`:

- Provides detailed information about the query execution plan.

Example
Here’s an example demonstrating the use of a cursor with `find()`:

```javascript

// Perform a query and get a cursor

var cursor = db.catalog.find({ year: 2020 });

// Iterate through the cursor

cursor.forEach(function(doc) {

printjson(doc);

});

```

In this example:

- `db.catalog.find({ year: 2020 })` returns a cursor.

- `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.

You might also like