How to Run Atlas Search String Queries Against Date and Numeric Fields
On this page
This tutorial describes how to run Atlas Search queries against
string
, date
, and number
fields in the
sample_airbnb.listingsAndReviews
collection. You will create a
materialized view that stores
the numeric and date field values as strings. You will then create an
Atlas Search index on the materialized view and run queries against these
string fields using the queryString and
autocomplete operators. This tutorial takes you through
the following steps:
Create a materialized view on the
sample_airbnb.listingsAndReviews
collectionname
,property_type
,last_scraped
, andaccomodates
fields.Set up dynamic and static Atlas Search indexes on the materialized view.
Run Atlas Search queries against the fields in the materialized view using the queryString and autocomplete operators to search for properties.
Before you begin, ensure that your Atlas cluster meets the requirements described in the Prerequisites.
Required Access
To create an Atlas Search index, you must have Project Data Access Admin
or higher access to the project.
Create a Materialized View on the Collection
In this section, you will create a materialized view named airbnb-mat-view
for name
,
property_type
, last_scraped
, accomodates
, and
maximum_nights
fields in the airbnb_listingsAndReviews
collection. The materialized view allows you to take the numeric and
date fields in the source collection and store them as string fields in
the materialized view.
Log in to Atlas and connect to your cluster using mongosh
.
Open mongosh
in a terminal window and connect to your
cluster. For detailed instructions on connecting, see
Connect via mongosh
.
Verify and switch to the sample_airbnb
database.
Run the following command to verify that the database exists in your cluster:
show dbs sample_airbnb 55.3 MB sample_analytics 9.59 MB sample_geospatial 1.43 MB sample_guides 41 kB sample_mflix 51.1 MB sample_restaurants 6.95 MB sample_supplies 1.21 MB sample_training 55.5 MB sample_weatherdata 2.89 MB admin 348 kB local 2.1 GB Run the following command to switch to the
sample_airbnb
database.use sample_airbnb switched to db sample_airbnb
Create a materialized view named airbnb_mat_view
.
To create a materialized view, run the following query. The query specifies the following aggregation pipeline stages:
$project
: In this stage, the query does the following:Converts the
last_scraped
date object to a string in the formatYYYY-MM-DD
using$dateToString
.Includes
name
andproperty_type
string fields.Converts
accomodates
number field to a string using$toString
.Converts
maximum_nights
number field to a string using$toString
.
$merge
: In this stage, the query writes the output fields from the$project
stage to a materialized view namedairbnb_mat_view
.db.listingsAndReviews.aggregate( [ { $project: { lastScrapedDate: { $dateToString: { format: "%Y-%m-%d", date: "$last_scraped" } }, propertyName: "$name", propertyType: "$property_type", accommodatesNumber: { $toString: "$accommodates" }, maximumNumberOfNights: { $toString: "$maximum_nights" } } }, { $merge: { into: "airbnb_mat_view", whenMatched: "replace" } } ] )
Create Atlas Search Indexes on Fields in the Materialized View
In this section, you will create Atlas Search indexes on the
lastScrapedDate
, name
, propertyType
,
accommodatesNumber
, and maximumNumberOfNights
fields for
running queries against these fields.
Navigate to the Atlas Search page for your project.
If it is not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.
If it is not already displayed, select your desired project from the Projects menu in the navigation bar.
Click your cluster's name.
Click the Search tab.
Enter the Index Name, and set the Database and Collection.
In the Index Name field, enter
date-number-fields-tutorial
.Note
If you name your index
default
, you don't need to specify anindex
parameter when using the $search pipeline stage. Otherwise, you must specify the index name using theindex
parameter.In the Database and Collection section, find the
sample_airbnb
database, and select theairbnb_mat_view
collection.
Define an index on the fields in the materialized view.
You can create one of the following indexes:
Index that uses dynamic mappings for running queries using the queryString operator. You can't run queries using the autocomplete operator if your index definition uses only dynamic mappings.
Index that uses static mappings for running queries using autocomplete operator. You can't run queries using the queryString operator against fields indexed as type
autocomplete
.
Perform Text Search on Converted Fields
You can run queries against the numeric and date fields that were converted to strings. This tutorial uses queryString and autocomplete operators to search for properties. The query uses the following pipeline stages:
$search
stage to search the collection$limit
stage to limit the output to5
results$project
stage to exclude_id
In this section, you will connect to your Atlas cluster and run
the sample queries using the operator against the fields in the
airbnb_mat_view
collection.
Note
➤ Use the Select your language drop-down menu on this page to set the language of the examples in this section.