In the previous blog post
we discussed using Tigris as a scalable, cost-effective, and open source
alternative to MongoDB Atlas. As a continuation of that, this blog post will
demonstrate using the MongoDB Shell mongosh
in a way that is transparent to the application that the data is stored in
Tigris Cloud database.
Furthermore, there will be no cluster to create and no sharding to worry
about.
We have also recorded a video if you prefer video content.
Prerequisites
- A Tigris Cloud account.
Create one for free. - mongosh installed on your computer.
- Tigris CLI. The installation instructions can be found
here.
Create the Tigris project and generate the application key
Login to Tigris Cloud:
Create a Tigris project and generate the application key. The project name
will be used as the MongoDB database name. The application key is used to
authenticate to Tigris.
tigris create project go_mongo_quickstart
tigris create app_key default --project mongosh_quickstart
The above command will have an output similar to the following:
{
"id": "your_client_id",
"name": "default",
"secret": "your_client_secret",
"created_at": 1676857746000,
"created_by": "google-oauth2|1111xxx",
"project": "mongosh_quickstart"
}
Using mongosh
Connect to Tigris
We will use the id
and secret
from the output of the command above as the
username
and password
in our URI. We will use
m1k.preview.tigrisdata.cloud
as the deployment to connect to.
info
Note how you did not have to create a MongoDB Atlas cluster.
m1k.preview.tigrisdata.cloud
is a serverless deployment with builtin
automatic database sharding, meaning that, unlike MongoDB Atlas, you have to
never worry about sharding or shard keys, and the data distribution is
automatically handled.
mongosh mongodb://your_client_id:your_client_secret@m1k.preview.tigrisdata.cloud/?authMechanism=PLAIN --quiet
You will see output similar to the following:
Create the database
Let’s create our database mongosh_quickstart
You will see output similar to following:
test> use mongosh_quickstart
switched to db mongosh_quickstart
Create a collection and insert a document
Let’s create the collection podcasts
and insert a document
db.podcasts.insertOne({
title: 'The Polyglot Developer Podcast',
author: 'Nic Raboy',
tags: [ 'development', 'programming', 'coding' ]
})
This results in the following output:
mongosh_q