Docs Menu

queryString

On this page

  • Definition
  • Syntax
  • Options
  • Examples
  • Basic Examples
  • Advanced Examples
queryString

The queryString operator supports querying a combination of indexed fields and values.

queryString has the following syntax:

1{
2 "$search": {
3 "index": <index name>, // optional, defaults to "default"
4 "queryString": {
5 "defaultPath": "<default-field-to-search>",
6 "query": "<field-to-search>:(<search-values> AND|OR <search-values>|<field-to-search>: <search-values>)"
7 }
8 }
9}

queryString uses the following terms to construct a query:

Field
Type
Description
Necessity
defaultPath
string

The indexed field to search by default. Atlas Search only searches the field in defaultPath:

  • If you omit the field to search in the query.
  • If a value you specified in the query is not found in the field you specified in the query.
yes
query
string

One or more indexed fields and values to search. Fields and values are colon-delimited. For example, to search the plot field for the string baseball, use plot:baseball. The following operators are available to combine multiple search criteria:

You can combine the fields and values using the following:

AND
Indicates AND boolean operator. All search values must be present for a document to be included in the results.
OR
Indicates OR boolean operator. At least one of the search value must be present for a document to be included in the results.
()
Delimiters for subqueries. Use the parentheses to group fields and values to search.
Tip
See also:

Examples for some sample queries that use the operators and delimiters to search the sample movies collection.

yes
score
object

The score assigned to matching search results. You can modify the score using the following options:

  • boost: multiply the result score by the given number.
  • constant: replace the result score with the given number.

For information on using score in your query, see Customize the Score in Results.

no

The following examples use the movies collection in the sample_mflix database. If you have the sample dataset on your cluster, you can create the Atlas Search default index and run the example queries on your cluster.

The following example uses the queryString operator to query for movie plots that contain the terms captain, kirk, and chess. This example shows how different grouping of the same search terms using parentheses can result in different documents to be included in the search results.

Example

The following queries search for a combination of the terms, captain, kirk, and chess in the plot field. Each query returns a different result depending on how the search terms are grouped.

The queries also includes a $project stage to:

  • Exclude all fields except title, plot, and fullpath
  • Add a field named score
←  phraserange →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.