FerretDB is an open-source database, providing a viable alternative to MongoDB, and is built on top of PostgreSQL. As an open-source proxy, it seamlessly translates MongoDB wire protocol queries into SQL, utilizing PostgreSQL as its robust database engine. With full compatibility with MongoDB drivers, FerretDB is a direct replacement for MongoDB 6.0+. It was created to be the open-source alternative to MongoDB, making it the preferred option for users seeking an open-source solution for MongoDB. By leveraging FerretDB, users can effortlessly execute MongoDB protocol queries without acquiring a new language or command knowledge.

In this blog, we will discuss FerretDB and how you can install it in the Kubernetes cluster using Civo Marketplace and how you can access it.

In this video @KunalKushwaha introduces FerretDB, an open-source alternative to MongoDB built on PostgreSQL. It explains how to install FerretDB through the Civo Marketplace, highlighting the prerequisites and installation process. The video covers basic CRUD operations using FerretDB, including creating, reading, updating, and deleting documents. It also mentions the benefits of using FerretDB, such as its open-source nature, transparency, simplicity, and supportive community.

Prerequisite

Before we start, there are some prerequisites you’ll need to have in place:

Installing FerretDB

You can install the FerretDB from the Civo Marketplace through UI or the Civo CLI.

By Using UI

After successfully creating the Kubernetes cluster, login to the Civo dashboard and go to Kubernetes > Kubernetes Cluster > Marketplace as shown in the screenshot below.

FerretDB on Civo Marketplace

Select the ‘Database’ tab in the Marketplace section and click ‘FerretDB.’ Click “Install Apps” from here to install the FerretDB application inside the cluster.

Once you click on FerretDB, you will be prompted to select the plan for your FerretDB application. Select it according to your specific needs and requirements.

By Using CLI

To install the Ferret DB using Civo CLI, you will need to run the command:

civo kubernetes applications --cluster <CLUSTER_NAME> add FerretDB:<PLAN>

For CLUSTER_NAME put the name of the cluster in which you want to install the FerretDB; for PLAN you can write 5GB, 10GB, and 15GB. If you don’t write the value for PLAN it automatically selects 5GB.

Note: During the installation of FerretDB via Civo Marketplace, a load balancer will be automatically created at an additional cost. It is important to be aware that separate charges will apply for the load balancer service.

Accessing FerretDB

To access or use FerretDB, you should have Mongo client (Mongosh) installed in your local system.

To connect with FerretDB, run the command given below in the terminal:

mongosh "mongodb://<username>:<password>@<dns-of-loadbalancer-associated-with-cluster>:27017?authMechanism=PLAIN"

To get the username and password, go to Kubernetes > Kubernetes Cluster > Installed Applications.

Click on FerretDB, you will see the username and password. Copy the credentials provided and use them in the command.

To get the DNS of the load balancer that is associated with the cluster, for this, go to Kubernetes > Kubernetes Cluster, then go to the load balancer section. Copy the DNS and paste it into the command.

If you don’t get any errors, you successfully connected to the FerretDB.

FerretDB Terminology

Before you get start performing operations on FerretDB, you should know some of the basic terminologies about FerretDB:

  • Documents: Documents are self-describing records that contain both the actual data being stored and a description of that data. In terms of relational databases, documents can be conceptually similar to rows in a table. Unlike relational databases, documents in document-oriented databases have flexible schemas.
  • Collections: Collections in FerretDB serve as repositories for documents and can be compared to tables in relational databases. When inserting documents into FerretDB, a new collection is automatically created if it doesn't already exist. Collections can hold one or more documents, allowing for the organization and storage of related data within the database.

FerretDB Operations

As we know, FerretDB supports MongoDB commands, the command use DATABASE_NAME is used to create a new database. It will automatically create a new database if it doesn't exist, otherwise returns the existing database.

Here we are creating a new database named merchant-db:

> use merchant-db
switched to db merchant-db

To check the currently selected database, use the command:

> db
merchant-db

CRUD Operations

Let’s perform the CRUD (Create, Read, Update, Delete) operations in FerretDB.

1. Create

The create operation is used to add a new document to a collection. If the specified collection does not exist, the create operation will create it automatically. This allows for dynamic creation of collections as new documents are added to the database.

Let’s see an example of how we can do this since the merchant-db database has already been created.

To insert a single document into the collection, use:

db.collection.insertOne()

To insert the detail of a single merchant into the collection named merchant, use:

db.merchant.insertOne(
{
name: 
{ 
firstName: "test", 
lastName: "test"
}, 
address: "test", 
email: "test@test.com", 
phoneNumber: "4567823465", 
age: 22, 
company: "civo"
}
);

To insert multiple documents into the collection at once, use:

db.collection.insertMany()

To insert the records of multiple merchants at once, use:

db.merchant.insertMany([
{
name: {
firstName: "Sam",
lastName: "Fisher",
},
address: "England",
email: "sam@test.com",
phoneNumber: "2346263298",
age: 27,
company: "civo",
},
{
name: {
firstName: "Penny",
lastName: "Cooper",
},
address: "USA",
email: "penny@test.com",
phoneNumber: "5674598736",
age: 23,
company: "facebook",
},
{
name: {
firstName:  "Harry",
lastName: "Watson",
},
address: "England",
email: "harry@test.com",
phoneNumber: "324545644",
age: 22,
company: "ferret",
},
{
name: {
firstName: "Mahesh",
lastName: "Kumar",
},
address: "India",
email: "mahesh@tests.com",
phoneNumber: "7897666111",
age: 24,
company: "google"
},
{
name: {
firstName: "Bert",
lastName: "Kamal",
},
address: "Spain",
email: "bert@test.com",
phoneNumber: "4356552223",
age: 29,
company: "facebook"
},
])

2. Read

The read operation is used to retrieve document records from a collection. It allows you to fetch documents based on specific criteria or filters. By specifying certain criteria, such as specific field values or conditions, you can narrow down the result set and retrieve only the documents that meet those criteria. This filtering capability provides flexibility and enables you to retrieve the data that is relevant to your specific needs or queries.

To retrieve all the documents in the collection, use:

db.collection.find()

To retrieve all the documents of merchant collection, use

db.merchant.find()

Using the find() command, we can also filter a collection for only the documents that match the provided query. For example, to get merchants whose age is 22, use:

db.merchant.find({age: 22})

For this, you will get the output like this:

[
  {
    _id: ObjectId("6486b1fb8a6643411e5ff488"),
    name: { firstOne: 'test', lastName: 'test' },
    address: 'test',
    email: 'test@test.com',
    phoneNumber: '4567823465',
    age: 22,
    company: 'civo'
  },
  {
    _id: ObjectId("6486c0aa8a6643411e5ff48c"),
    name: { firstName: 'Harry', lastName: 'Watson' },
    address: 'England',
    email: 'harry@test.com',
    phoneNumber: '324545644',
    age: 22,
    company: 'ferret'
  }
]

To retrieve a single document from the collection, use:

db.collection.findOne()

As we want to retrieve the document, whose email is test@test.com, from the merchant collection, use:

db.merchant.findOne({email: ‘test@test.com’})

You will get the output like this:

{
  _id: ObjectId("6486b1fb8a6643411e5ff488"),
  name: { firstOne: 'test', lastName: 'test' },
  address: 'test',
  email: 'test@test.com',
  phoneNumber: '4567823465',
  age: 22,
  company: 'civo'
}

3. Update

The update operation in a database system is used to modify document records within a collection. It allows you to make changes to existing documents based on specified query criteria. By using the update operation, you can keep your data up-to-date and ensure that your collection contains accurate information.

If we want to update only a single document, use:

db.collection.updateOne()

If we have inserted the wrong phone number of the merchant whose email is bert@test.com and we want to update the phone number, use:

db.merchant.updateOne(
{
email: 'bert@test.com'
},
{
$set: {
phoneNumber: '4678972342'
}
}
)

To modify multiple documents at once, use:

db.collection.updateMany()

A company name Facebook is changed to Meta, and we want to update all the documents of the merchant whose company name is Facebook, for this use:

db.merchant.updateMany(
{
company: "facebook"
},
{
$set:{
company: "meta"
}
}
)

4. Delete

The delete operation is used to remove document records from a collection. It allows you to delete documents based on specified criteria selectively. By providing a query that matches specific documents in the collection, you can identify the documents to be removed. The delete operation then removes those matched documents from the collection.

To delete only one document from the collection, use:

db.collection.deleteOne()

To delete the record of the merchant whose email is bert@test.com, use:

db.merchant.deleteOne({email: "bert@test.com"});

To delete multiple documents at once, use:

db.collection.deleteMany()

To delete the records of the merchant whose age is 22, use:

db.merchant.deleteMany({age: 22});

The Benefits of FerretDB

FerretDB provides various advantages that make it an attractive alternative to MongoDB, including:

  • Open source: Being licensed under the Apache 2.0 license, FerretDB is not only free to use but also allows users to modify and distribute the software. This open-source nature encourages collaboration, fosters innovation, and provides users with the freedom to tailor FerretDB to their specific needs.
  • Transparent: FerretDB is designed with application transparency in mind. This means that applications built for MongoDB can seamlessly migrate to FerretDB without any changes. The compatibility and consistency with the MongoDB wire protocol ensure a smooth transition, saving time and effort during the migration process.
  • Simplicity: FerretDB prioritizes simplicity, making it accessible for developers, even those who are new to document databases. The intuitive design and user-friendly interface enable users to quickly grasp the concepts and functionalities of FerretDB, allowing for efficient and straightforward usage.
  • Community: FerretDB boasts a growing community of passionate users and developers who are actively involved in its development and improvement. This community-driven approach ensures that FerretDB continues to evolve, benefitting from valuable feedback, bug fixes, feature enhancements, and ongoing support. The community's collective efforts make FerretDB a reliable and robust database solution.

Conclusion

In this tutorial, we have discussed the installation of the FerretDB via the Civo Marketplace. We have explored the process of accessing and utilizing the FerretDB, along with its associated benefits and limitations.

If you want to know more about FerretDB, please take a look at these resources: