0% found this document useful (0 votes)
3 views2 pages

Exp 9

The document outlines how to perform text searches in a MongoDB catalog collection, including creating a collection, inserting sample documents, and creating a text index. It demonstrates how to search for documents containing a specific word, as well as how to exclude documents with certain words or phrases. Various queries are provided to illustrate these text search functionalities, including the use of regular expressions for exclusion.

Uploaded by

Pra Nav
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)
3 views2 pages

Exp 9

The document outlines how to perform text searches in a MongoDB catalog collection, including creating a collection, inserting sample documents, and creating a text index. It demonstrates how to search for documents containing a specific word, as well as how to exclude documents with certain words or phrases. Various queries are provided to illustrate these text search functionalities, including the use of regular expressions for exclusion.

Uploaded by

Pra Nav
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/ 2

9.a.

Develop a query to demonstrate Text search using catalog data collection for a given
word.

1: Create the catalog Collection and Insert Sample Documents

use mydb
>db.createCollection("catalog")
>db.catalog.insertMany([
{
title: "Mastering MongoDB",
description: "A comprehensive guide to MongoDB for developers."
},
{
title: "Node.js Essentials",
description: "Learn backend development with Node.js."
},
{
title: "MongoDB Cookbook",
description: "Over 100 recipes for MongoDB developers."
},
{
title: "Introduction to Databases",
description: "Fundamentals of database systems."
}
])
2: Create a Text Index
>db.catalog.createIndex(
{ title: "text", description: "text" }
)
3: Perform a Text Search
>db.catalog.find({ $text: { $search: "MongoDB" } })

Output:
mydb> ... ... title_text_description_text
mydb> [
{
_id: ObjectId('6819e6964a2a0438dc6b128e'),
title: 'MongoDB Cookbook',
description: 'Over 100 recipes for MongoDB developers.'
},
{
_id: ObjectId('6819e6964a2a0438dc6b128c'),
title: 'Mastering MongoDB',
description: 'A comprehensive guide to MongoDB for developers.'
}
]
b. Develop queries to illustrate excluding documents with certain words and phrases

1. Exclude Documents Containing a Specific Word


> db.catalog.find({ $text: { $search: "-MongoDB" } })
>> This query will return documents that do not contain the word "MongoDB" in the fields covered by
the text index.

2. Exclude Documents Containing a Specific Phrase


>db.catalog.find({ $text: { $search: "-\"backend development\"" } })

3. Exclude Documents Containing Multiple Specific Words


>db.catalog.find({ $text: { $search: "-MongoDB -Node.js" } })

4. Exclude Documents Using Regular Expressions


> db.catalog.find({ title: { $not: /MongoDB/i } })
>> This query performs a case-insensitive search and excludes documents with "MongoDB" in the title
field.

You might also like