If you haven't already sone so, create an Atlas cluster.
Make sure at least 1 user with read-write access has been added.
Make sure that IP Access List allows access (add Allow ACCESS FROM ANYWHERE(0.0.0.0\0) to be sure).
Add sample data:
- Click the
...next to your cluster in the Atlas UI - Select
Load Sample Dataset
It will take a few minutes for the sample data to be added.
- Keep the name as
default - Databse:
sample_mflix - Collection:
movies - Leave
Dynamic Mappingturned on
- Set the name to
autocomplete - Turn off
Dynamic Mapping - Click
Refine Your Index - Add a new field mapping for the
titlefield with aData TypeofAutocomplete
You can get the URL from the Atlas UI - click the Connect button next to your cluster.
Paste the URL into the Compass connection box, replacing the username and password.
From Compass (or the Atlas UI), navigate to the Aggregation pane for sample_mflix.movies.
Create these stages in sequence:
{
index: 'default',
text: {
query: 'Indiana Ones',
path: ['plot', 'fullplot', 'title'],
fuzzy: {}
},
highlight: {path: 'fullplot'}
}12
{
title: 1,
year: 1,
poster: 1,
plot: 1,
imdb: 1,
score: {$meta: 'searchScore'},
highlights: {
'$meta': 'searchHighlights'
}
}- Click
EXPORT TO LANGUAGE - Choose
NODEas the export language - Copy the code and keep it safe
- Save the pipeline if using Compass
- Create a new Atlas App Service and name it
Movies. - Select
Functionsand thenCreate New Function. - Set
AuthenticationtoSystem, and thenSave Draft. - Use this template for the function code:
exports = async function({ query, headers, body}, response) {
// GET A HANDLE TO THE MOVIES COLLECTION
const moviesCollection = context.services.get("mongodb-atlas").db("sample_mflix").collection("movies");
// GET SEARCHTERM FROM QUERY PARAMETER. IF NONE, RETURN EMPTY ARRAY
let searchTerm = query.searchTerm;
if (!query.searchTerm || searchTerm === ""){
return [];
}
const searchAggregation = [];
return await moviesCollection.aggregate(searchAggregation).toArray();
};- Replace the
[]forsearchAggregationwith your aggregation pipeline - Replace
Indiana OneswithsearchTerm Save Draft- Select
HTTPS Endpoints Add An EndpointRoute: `/movies'HTTP Method:GET- Enable
Respond With Result Function: the function you just createdSave DraftREVIEW DRAFT & DEPLOY- Copy the URL ->
<baseURL> - From a browser, paste the URL into the location bar (
<baseURL>?searchTerm=blues). This will search for movies that mentionblues- replace it with your own string (using%20for spaces - e.g.,blues%20brothers)
From Compass (or the Atlas UI), navigate to the Aggregation pane for sample_mflix.movies.
Create these stages in sequence:
{
index: 'autocomplete',
autocomplete: {
query: 'Indiana J',
path: 'title'
}
}10{
_id: 0,
title: 1
}- Open your
MoviesAtlas App Service. - Select
Functionsand thenCreate New Function. - Set
AuthenticationtoSystem, and thenSave Draft. - Use this template for the function code:
exports = async function({ query, headers, body}, response) {
// GET A HANDLE TO THE MOVIES COLLECTION
const moviesCollection = context.services.get("mongodb-atlas").db("sample_mflix").collection("movies");
// GET SEARCHTERM FROM QUERY PARAMETER. IF NONE, RETURN EMPTY ARRAY
let searchTerm = query.searchTerm;
if (!query.searchTerm || searchTerm ===""){
return [];
}
const searchAggregation =[];
return await moviesCollection.aggregate(searchAggregation).toArray();
};- Replace the
[]forsearchAggregationwith your aggregation pipeline - Replace
Indiana OneswithsearchTerm Save Draft- Select
HTTPS Endpoints Add An EndpointRoute: `/titles'HTTP Method:GET- Enable
Respond With Result Function: the function you just createdSave DraftREVIEW DRAFT & DEPLOY- Copy the URL ->
<baseURL> - From a browser, paste the URL into the location bar (
<baseURL>?searchTerm=blues). This will search for movies that mentionblues- replace it with your own string (using%20for spaces - e.g.,blues%20brothers)
This repo includes a REACT movie-search web app which is able to use the two HTTP endpoints you've just created.
To download and start the app (if you're able to install Node.js on your machine):
git clone git@github.com:ClusterDB/AtlasSearchWorkshop.git
cd AtlasSearchWorkshop
npm install
npm startTo download and start the app (if you're not able to install Node.js on your machine):
Create a CodeSpace (you must be logged in to GitHub):
You should then wait for the installation to complete in the terminal window. Once complete, start the app by running npm start from the CodeSpace terminal.
The web app should open in a browser window. If you try searching for anything, you should get a message that you need to set up the HTTPS endpoints.
To make the app work, you need to add your endpoints to src/components/Home.js (lines 23 & 24):
// INSERT YOUR CREATED MOVIE ENDPOINTS
const [moviesEndpoint, setMoviesEndpoint] = useState('Your endpoint to fetch movies goes here');
const [titlesEndpoint, setTitlesEndpoint] = useState('Your endpoint to fetch titles goes here');When you save the files, the app should rebuild - you may need to refresh the browser window.
If you're not able to build the frontend Node.js app locally or in CodeSpace, you can still test out your Atlas backend by pasting your endpoints into this shared frontend app: https://application-0-jdvvr.mongodbstitch.com/.
A quick demo of some of the features of Atlas Search (including the queries) using Restaurant Finder. Code can be found here.

