0% found this document useful (0 votes)
2 views6 pages

Program 10_WM

The document outlines the steps to create a MongoDB database named TextDB and a collection called catalog, including adding sample product documents. It details how to create a text index on the 'name' and 'description' fields and provides an aggregation pipeline for performing a text search on the catalog collection. The example pipeline matches documents containing a specific search term and projects the required fields in the output.

Uploaded by

nainitharao.b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Program 10_WM

The document outlines the steps to create a MongoDB database named TextDB and a collection called catalog, including adding sample product documents. It details how to create a text index on the 'name' and 'description' fields and provides an aggregation pipeline for performing a text search on the catalog collection. The example pipeline matches documents containing a specific search term and projects the required fields in the output.

Uploaded by

nainitharao.b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Program 10

Develop an aggregation pipeline to illustrate Text search on Catalog data collection.

Create a database TextDB and collection catalog in Mongo IDE.

Add the following documents in the catalog collection in MongoDB Shell.

"name": "Apple iPhone 14",

"description": "Latest model of iPhone with advanced features",

"category": "Electronics"

}
{

"name": "Samsung Galaxy S21",

"description": "Newest Samsung smartphone with great camera",

"category": "Electronics"

}
{

"name": "Sony Headphones",

"description": "Noise-cancelling headphones for immersive sound",

"category": "Audio"

In MongoShell

>use TextDB

Create a Text Index

First, create a text index on the ‘name’ and ‘description’ fields.

db.catalog.createIndex({ name: "text", description: "text" });

Output:
Define the Aggregation Pipeline

Now, create an aggregation pipeline to perform the text search and process the results. Below
is an example pipeline:

db.catalog.aggregate([
// Stage 1: Match documents containing the search term
{
$match: {
$text: { $search: " Apple iPhone 14" }
}
},
// Stage 2: Project only required fields
{
$project: {
_id: 0,
name: 1,
description: 1,
category: 1

// Add more fields as needed


}
}
])

Output:

You might also like