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