phrase - Atlas - MongoDB Docs (2025)

Docs Home

/

Atlas

/

Atlas Search

/

/

Query Reference

/

Operators and Collectors

Definition

phrase

The phrase operator performs search for documents containing anordered sequence of terms using the analyzerspecified in the index configuration.If no analyzer is specified, the default standard analyzer is used.

Syntax

phrase has the following syntax:

1{
2 $search: {
3 "index": <index name>, // optional, defaults to "default"
4 "phrase": {
5 "query": "<search-string>",
6 "path": "<field-to-search>",
7 "score": <options>,
8 "slop": <distance-number>,
9 "synonyms": "<synonyms-mapping-name>"
10 }
11 }
12}

Options

phrase uses the following terms to construct a query:

Field

Type

Description

Necessity

query

string or array of strings

String or strings to search for.

yes

path

string or array of strings

Indexed field or fields to search. You can also specify awildcard path to search.

IMPORTANT: To use the phrase operator, you must index the query field as type string with the indexOptions propertyset to positions or the default value, offsets. To learn more, see Configure String Field Properties.

yes

slop

integer

Allowable distance between words in the query phrase. Lowervalue allows less positional distance between the words andgreater value allows more reorganization of the words and moredistance between the words to satisfy the query. The default is0, meaning that words must be exactly in the same positionas the query in order to be considered a match. Exact matchesare scored higher.

no

score

object

Score to assign to matching search results. You can modify thedefault score using the following options:

  • boost: multiply the result score by the given number.

  • constant: replace the result score with the given number.

  • function: replace the result score with the given expression.

When querying array values, Atlas Search assigns the same scoreregardless of how many values in the array match the query.

For information on the options for modifying the default score,see Score the Documents in the Results.

no

synonyms

string

Required for running queries using synonyms.

Name of the synonym mapping definition inthe index definition. Value can'tbe an empty string.

The amount of time that Atlas Search takes to execute queries thatuse synonym mappings depends on thenumber and size of documents in the synonym sourcecollection.

For example, a query that uses a synonym mapping that is basedon very few synonym documents might be faster than a querythat uses a synonym mapping that is based on many synonymdocuments.

Optional

Examples

The examples in this page use the movies collection in thesample_mflix database. After loading the sample dataset into your cluster, create the Atlas Searchindex with dynamic mappings and run the example queries on yourcluster. To try the synonyms examples,you must also add the synonymous_terms collection to the sample_mflix database and thendefine the index with thesynonyms mapping collection.

Single Phrase Example

The following Atlas Search example performs a basic search of thetitle field for the query string new york. There is noslop in the query and so the slop value defaults to0, which means the position of the words must exactly matchthe query string to be included in the results. The query alsoincludes a:

  • $limit stage to limit the output to 10 results.

  • $project stage to exclude all fields excepttitle and add a field named score.

Basic Example

The following query returns the documents that match the searchcriteria.

1db.movies.aggregate([
2 {
3 "$search": {
4 "phrase": {
5 "path": "title",
6 "query": "new york"
7 }
8 }
9 },
10 { $limit: 10 },
11 {
12 $project: {
13 "_id": 0,
14 "title": 1,
15 score: { $meta: "searchScore" }
16 }
17 }
18])
1[
2 { title: 'New York, New York', score: 6.786321640014648 }
3 { title: 'New York', score: 6.258549213409424 }
4 { title: 'New York Stories', score: 5.3813982009887695 }
5 { title: 'New York Minute', score: 5.3813982009887695 }
6 { title: 'Synecdoche, New York', score: 5.3813982009887695 }
7 { title: 'New York Doll', score: 5.3813982009887695 }
8 { title: 'Little New York', score: 5.3813982009887695 }
9 { title: 'Escape from New York', score: 4.719893455505371 }
10 { title: 'Naked in New York', score: 4.719893455505371 }
11 { title: 'Autumn in New York', score: 4.719893455505371 }
12]

Metadata Example

The following query returns the metadata results for the searchcriteria. It uses the $searchMeta stage to get thenumber of movies in the following buckets (years) that match the searchcriteria:

  • 2000, inclusive lower bound for this bucket

  • 2005, exclusive upper bound for the 2000 bucket and inclusive lower bound for this bucket

  • 2010, exclusive upper bound for the 2005 bucket and inclusive lower bound for this bucket

  • 2010, exclusive upper bound for the 2010 bucket

Example

1db.movies.aggregate([
2 {
3 "$searchMeta": {
4 "facet": {
5 "operator": {
6 "phrase": {
7 "path": "title",
8 "query": "new york"
9 }
10 },
11 "facets": {
12 "yearFacet": {
13 "type": "number",
14 "path": "year",
15 "boundaries": [2000, 2005, 2010, 2015 ]
16 }
17 }
18 }
19 }
20 }
21])
1[
2 {
3 count: { lowerBound: Long('27') },
4 facet: {
5 yearFacet: {
6 buckets: [
7 { _id: 2000, count: Long('3') },
8 { _id: 2005, count: Long('8') },
9 { _id: 2010, count: Long('4') }
10 ]
11 }
12 }
13 }
14]

The results show that there are 3 movies between 2000 and 2005, 8movies between 2005 and 2010, and 4 movies between 2010 and 2015 thatcontain the term new york in the title.

Multiple Phrases Example

The following Atlas Search example performs a basic search of thetitle field for the query strings the man and themoon. There is no slop in the query and so the slopvalue defaults to 0, which means the position of the wordsmust exactly match the query string to be included in theresults. The query also includes a:

  • $limit stage to limit the output to 10.

  • $project stage to exclude all fields excepttitle and add a field named score.

1db.movies.aggregate([
2 {
3 "$search": {
4 "phrase": {
5 "path": "title",
6 "query": ["the man", "the moon"]
7 }
8 }
9 },
10 { $limit: 10 },
11 {
12 $project: {
13 "_id": 0,
14 "title": 1,
15 score: { $meta: "searchScore" }
16 }
17 }
18])
1[
2 { title: 'The Man in the Moon', score: 4.4830474853515625 },
3 { title: 'Shoot the Moon', score: 3.252699851989746 },
4 { title: 'Kick the Moon', score: 3.252699851989746 },
5 { title: 'The Man', score: 2.8923356533050537 },
6 { title: 'The Moon and Sixpence', score: 2.8528637886047363 },
7 { title: 'The Moon Is Blue', score: 2.8528637886047363 },
8 { title: 'Racing with the Moon', score: 2.8528637886047363 },
9 { title: 'Mountains of the Moon', score: 2.8528637886047363 },
10 { title: 'Man on the Moon', score: 2.8528637886047363 },
11 { title: 'Castaway on the Moon', score: 2.8528637886047363 }
12]

Slop Example

The following Atlas Search example performs a search of the title fieldfor the query string men women. The slop value of 5 in thequery allows greater movement of the words and distance between thewords men and women. The query includes a $projectstage to:

  • Exclude all fields except title

  • Add a field named score

1db.movies.aggregate([
2 {
3 "$search": {
4 "phrase": {
5 "path": "title",
6 "query": "men women",
7 "slop": 5
8 }
9 }
10 },
11 {
12 $project: {
13 "_id": 0,
14 "title": 1,
15 score: { $meta: "searchScore" }
16 }
17 }
18])
1[
2 { title: 'Men Without Women', score: 3.367523193359375 },
3 { title: 'Men Vs Women', score: 3.367523193359375 },
4 { title: 'Good Men, Good Women', score: 2.8529787063598633 },
5 { title: 'The War Between Men and Women', score: 2.1851978302001953 },
6 { title: 'Women Without Men', score: 1.9656763076782227 },
7 { title: 'Women Vs Men', score: 1.9656763076782227 }
8]

Synonyms Example

The following queries search the plot field in the moviescollection for the terms in the given query string. Atlas Search returnsresults based on the type of mapping in thesynonym source collection,synonymous_terms, specified in the synonym mapping definition of theindex for the sample_mflix.movies collection.

The following query searches for the phrase automobile race anywherein the plot field with a distance of up to 5 between the terms.

db.movies.aggregate([
{
$search: {
"phrase": {
"path": "plot",
"query": "automobile race",
"slop": 5,
"synonyms": "my_synonyms"
}
}
},
{
"$limit": 5
},
{
$project: {
"_id": 0,
"plot": 1,
"title": 1,
score: { $meta: "searchScore" }
}
}
])
[
{
plot: 'When a popular daredevil proposes an automobile race across three continents, his arch rival vows to beat him, while an ambitious female reporter has her own plans for victory.',
title: 'The Great Race',
score: 29.569732666015625
},
{
plot: 'A wide variety of eccentric competitors participate in a wild and illegal cross-country car race.',
title: 'The Cannonball Run',
score: 25.50379180908203
},
{
plot: 'A mechanic takes his family to a car race and a series of events occur which brings problems, betrayals, violence and the unexpected death of an elderly person.',
title: 'National Mechanics',
score: 21.538257598876953
},
{
plot: "It's time for the annual London to Brighton antique car rally, and Alan McKim and Ambrose Claverhouse are not going to let their friendship stop them from trying to humiliate each other. ...",
title: 'Genevieve',
score: 20.19266128540039
},
{
plot: "A naive drifter runs away from his army father in hopes of making it on the car racing circuit. In Las Vegas, he meets a young scam artist, who develops a crush on him. He is then ...",
title: 'Speedway Junky',
score: 18.639965057373047
}
]

The documents in the results contain the following terms in the plotfield with up to a distance of 5 between the terms:

  • automobile, car, or vehicle, which are defined asequivalent synonyms in the synonymous_terms collection,

  • race, contest, or rally, which are defined asexplicit synonyms in the sy``synonymous_terms`` collection,

Atlas Search returns similar results for a search for car race or vehiclerace because we configured automobile, car, and vehicle tobe equivalent synonyms. However, the results for automobilecontest wouldn't include documents with race or rally and theresults for automobile rally wouldn't include documents withrace or contest because we didn't configure contest orrally to be synonym of any terms.

Backnear
NextqueryString
phrase - Atlas - MongoDB Docs (2025)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Tyson Zemlak

Last Updated:

Views: 6483

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Tyson Zemlak

Birthday: 1992-03-17

Address: Apt. 662 96191 Quigley Dam, Kubview, MA 42013

Phone: +441678032891

Job: Community-Services Orchestrator

Hobby: Coffee roasting, Calligraphy, Metalworking, Fashion, Vehicle restoration, Shopping, Photography

Introduction: My name is Tyson Zemlak, I am a excited, light, sparkling, super, open, fair, magnificent person who loves writing and wants to share my knowledge and understanding with you.