This is a MongoDB Atlas App Services demo which shows how to implement several concepts of the Atlas App Services in combination with the powerful lucene based Atlas Search feature.
You can also find a quick overview by watching my presentation at MongoDB .local in Frankfurt. Click here for the video on YouTube
There is (still in german) a YouTube tutorial available which shows step by step how to build and use this app. You can find them here. English speaking audience: Please use subtitle to follow the tutorial. English version is in production ;-)
MongoDB Realm was renamed in summer 2022 to Atlas App Services
The following steps and features are available:
- Create testdata
- Merge data from two collections into one
- Scheduled Triggers and Functions (calculate age from birthday)
- Database Triggers and Functions (calculate total balance when account balance changes)
- First steps in Atlas App Services
- Imlementation of AG-Grid
- GraphQL
- Pagination
- Rowcount
- Introduction to Atlas Search
- Live updates using Server Sent Events SSE
- Advanced Search
- Fast Count and Facets
Everything works on an Atlas Free Tier. No Credit Card needed, free forever
- Clone this repo and go to the directory
git clone https://github.com/phil2211/MDBMasterclassTutorialApp.git && \
cd MDBMasterclassTutorialApp
- You need the following prerequisits met to follow along
- MongoDB Atlas Account (Free account available, no credit card needed)
- Atlas CLI
- MongoShell
- MongoDB Database Tools.
- NodeJS
I use Homebrew to do this on MacOS. If you don't have Homebrew, please follow the very simple instructions on the Hombrew website to install it. The command below will install all necessary tools at once. If you are using Windows or Linux, see the links above for installation instructions for each component.
brew tap mongodb/brew && \
brew install mongodb-atlas-cli mongodb-database-tools node npm
- (optional) Generate the autocompletion script for your shell. Learn more following this link. Here the example for MacOS
atlas completion zsh > $(brew --prefix)/share/zsh/site-functions/_atlas
- Install mgenerate and the App Services CLI
npm install -g mgeneratejs atlas-app-services-cli
- Restart your shell to use it
You can now follow the next steps manually or you just can execute the install.sh script to setup everything automatically
- Login to your Atlas account and choose your default project
atlas auth login -P MDBMasterclass
- Create a new project and a free cluster named MyCustomers
atlas projects create MDBMasterclass -P MDBMasterclass && \
atlas config set -P MDBMasterclass project_id `atlas -P MDBMasterclass project ls | grep MDBMasterclass | awk '{ print $1 }'` && \
atlas quickstart --skipMongosh --skipSampleData --provider AWS --region EU_CENTRAL_1 --tier M0 --username admin --password Passw0rd --accessListIp "0.0.0.0/0" --clusterName MyCustomers -P MDBMasterclass --force
- Wait until the new created cluster is ready. Check for the state to become "IDLE"
atlas cluster get MyCustomers -P MDBMasterclass
- Load testdata to your cluster using the loadTestdata.sh script. Run the following command with your username password and the clusterId from your new created cluster
sh testData/loadTestdata.sh admin Passw0rd $(atlas cluster connectionstrings describe MyCustomers -P MDBMasterclass | grep "mongodb+srv" | awk -F. '{print $2}')
- Pre-calculate the age and totalBalance field by using the following MongoDB query in the mongoshell
mongosh $(atlas cluster connectionstrings describe MyCustomers -P MDBMasterclass | grep "mongodb+srv")/MyCustomers --apiVersion 1 --username admin --password Passw0rd --eval 'db.customerSingleView.updateMany(
{},
[{
$set:
{
"totalBalance": {"$sum": "$accounts.balance"},
"age": {
"$subtract": [
{"$subtract": [{"$year": "$$NOW"}, {"$year": "$birthdate"}]},
{"$cond": [
{"$gt": [0, {"$subtract": [{"$dayOfYear": "$$NOW"},{"$dayOfYear": "$birthdate"}]}]},
1,
0
]}
]
}
}
}]
);'
- Create Atlas API keys for the appservices login
atlas project apiKeys create --desc appservices --role GROUP_OWNER -P MDBMasterclass > AtlasAPIKeys.txt
- Login your appservices using the new generated API-Key
appservices login --api-key $(cat AtlasAPIKeys.txt | grep "Public API Key" | awk '{ print $4 }') --private-api-key $(cat AtlasAPIKeys.txt | grep "Private API Key" | awk '{ print $4 }') -y --profile MDBMasterclass
- Import the backend code to Atlas using the appservices (Please select MDBMasterclass as target project when prompted)
appservices push --local "backend/MyCustomersGridApp" --include-package-json -y --profile MDBMasterclass && \
echo "REACT_APP_REALMAPP="$(appservices apps list --profile MDBMasterclass | grep mycustomersgridapp | awk '{print $1}') > frontend/.env.local
- Create an App user
appservices users create --type email --email test@example.com --password Passw0rd --profile MDBMasterclass -a $(appservices apps list --profile MDBMasterclass | grep mycustomersgridapp | awk '{print $1}')
- Create an Atlas Search index using the content of the following file
atlas clusters search indexes create -P MDBMasterclass -f "testData/AtlasSearchDefinitions/customEnhanced.json" --clusterName MyCustomers
- Install all dependencies for the frontend
cd frontend && \
npm install
- Start your frontend and login
npm start
- Wait for your browser to show the login page and log in using these credentials:
- User:
test@example.com
- Password:
Passw0rd