MongoDB Atlas comes with a full-text search engine that can be used to search for documents in a collection.
Atlas Search is powered by Apache Lucene.
We'll use the Atlas dashboard to create an index on the "sample_mflix" database from the sample data that we loaded in the Intro to Aggregations section.
$search
pipeline stage.sample_mflix
database and the movies
collection.To use our search index, we will use the $search
operator in our aggregation pipeline.
db.movies.aggregate([
{
$search: {
index: "default", // optional unless you named your index something other than "default"
text: {
query: "star wars",
path: "title"
},
},
},
{
$project: {
title: 1,
year: 1,
}
}
])
Try it Yourself »
The first stage of this aggregation pipeline will return all documents in the movies
collection that contain the word "star" or "wars" in the title
field.
The second stage will project the title
and year
fields from each document.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!