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:
tigris login
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.
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 never have to
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:27018/?authMechanism=PLAIN --quiet --tls
You will see output similar to the following:
test>
Create the databaseโ
Let's create our database mongosh_quickstart
use 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_quickstart> db.podcasts.insertOne({ title: 'The Polyglot Developer Podcast', author: 'Nic Raboy', tags: [ 'development', 'programming', 'coding' ] })
{
acknowledged: true,
insertedId: ObjectId("63f8340c6fdb0db31a94bcbf")
}
We can see that the collection is created:
mongosh_quickstart> show collections
podcasts
Read all documents in the collectionโ
Let's now read the data in the collection:
db.podcasts.find()
This returns all the documents in the collection podcasts
:
mongosh_quickstart> db.podcasts.find()
[
{
_id: ObjectId("63f8340c6fdb0db31a94bcbf"),
title: 'The Polyglot Developer Podcast',
author: 'Nic Raboy',
tags: [ 'development', 'programming', 'coding' ]
}
]
And that's all. Feel free to explore Deletes and Updates similarly.