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

Exp 10

The document outlines how to perform text search on a MongoDB collection named 'catalog' using an aggregation pipeline. It includes steps for creating a text index, constructing the aggregation pipeline with a text search, and applying $limit and $skip operations. Additionally, it highlights restrictions when using the $text operator in the aggregation pipeline.

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)
10 views2 pages

Exp 10

The document outlines how to perform text search on a MongoDB collection named 'catalog' using an aggregation pipeline. It includes steps for creating a text index, constructing the aggregation pipeline with a text search, and applying $limit and $skip operations. Additionally, it highlights restrictions when using the $text operator in the aggregation pipeline.

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

10.

Aggregation Pipeline to illustrate Text search on Catalog data collection

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."
}
])

1. Create a Text Index

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

2. Construct the Aggregation Pipeline

db.catalog.aggregate([
{ $match: { $text: { $search: "laptop" } } },
{ $project: {
name: 1,
description: 1,
price: 1,
score: { $meta: "textScore" }
}
},
{ $sort: { score: -1 } }
]);
* write the queries for the following:
$limit: Limit the output to 5 results.
$skip: Skip the first two results.
Restrictions

When using $text in the aggregation pipeline, be aware of the following restrictions:

 The $match stage containing $text must be the first stage in the pipeline.
 Only one $text expression is allowed in the $match stage.
 The $text operator cannot be used within $or or $not expressions.
 By default, the text search does not return documents in order of relevance score; you must
explicitly sort by the textScore.

You might also like