Understanding FerretDB

FerretDB is an open-source proxy that translates MongoDB wire protocol queries to SQL, with PostgreSQL or SQLite as the database engine. It uses the same commands, drivers, and tools as MongoDB.

Understanding FerretDB - 图1tip

New to FerretDB?

Check out our:

Supported backends

Understanding FerretDB - 图2caution

FerretDB is under constant development. As with any database, before moving to production, please verify if it is suitable for your application.

PostgreSQL

PostgreSQL backend is our main backend and is fully supported.

PostgreSQL should be configured with UTF8 encoding and one of the following locales: POSIX, C, C.UTF8, en_US.UTF8.

MongoDB databases are mapped to PostgreSQL schemas in a single PostgreSQL database that should be created in advance. MongoDB collections are mapped to PostgreSQL tables. MongoDB documents are mapped to rows with a single JSONB column. Those mappings will change as we work on improving compatibility and performance, but no breaking changes will be introduced without a major version bump.

SQLite

We also support the SQLite backend.

MongoDB databases are mapped to SQLite database files. MongoDB collections are mapped to SQLite tables. MongoDB documents are mapped to rows with a single JSON1 column. Those mappings will change as we work on improving compatibility and performance, but no breaking changes will be introduced without a major version bump.

SAP HANA (alpha)

Currently, we are also working with SAP on HANA compatibility. It is not officially supported yet.

Documents

Documents are self-describing records containing both data types and a description of the data being stored. They are similar to rows in relational databases. Here is an example of a single document:

  1. {
  2. first: "Thomas",
  3. last: "Edison",
  4. invention: "Lightbulb",
  5. birth: 1847
  6. }

The above data is stored in a single document.

Understanding FerretDB - 图3note

FerretDB follows almost the same naming conventions as MongoDB. However, there are a few restrictions, which you can find here.

For complex documents, you can nest objects (subdocuments) inside a document.

  1. {
  2. name: {
  3. first: "Thomas",
  4. last: "Edison"
  5. },
  6. invention: "Lightbulb",
  7. birth: 1847
  8. }

In the example above, the name field is a subdocument embedded into a document.

Dot notation

Dot notations (.) are used to reference a field in an embedded document or its index position in an array.

Arrays

Dot notations can be used to specify or query an array by concatenating a dot (.) with the index position of the field.

  1. 'array_name.index'

Understanding FerretDB - 图4note

When using dot notations, the field name of the array and the specified value must be enclosed in quotation marks.

For example, let’s take the following array field in a document:

  1. animals: ['dog', 'cat', 'fish', 'fox']

To reference the fourth field in the array, use the dot notation "animals.3".

Here are more examples of dot notations on arrays:

Embedded documents

To reference or query a field in an embedded document, concatenate the name of the embedded document and the field name using the dot notation.

  1. 'embedded_document_name.field'

Take the following document, for example:

  1. {
  2. name:{
  3. first: "Tom",
  4. last: "Barry"
  5. },
  6. contact:{
  7. address:{
  8. city: "Kent",
  9. state: "Ohio"
  10. },
  11. phone: "432-124-1234"
  12. }
  13. }

To reference the city field in the embedded document, use the dot notation "contact.address.city".

For dot notation examples on embedded documents, see here:

Collections

Collections are a repository for documents. To some extent, they are similar to tables in a relational database. If a collection does not exist, FerretDB creates a new one when you insert documents for the first time. A collection may contain one or more documents. For example, the following collection contains three documents.

  1. {
  2. Scientists: [
  3. {
  4. first: 'Alan',
  5. last: 'Turing',
  6. born: 1912
  7. },
  8. {
  9. first: 'Thomas',
  10. last: 'Edison',
  11. birth: 1847
  12. },
  13. {
  14. first: 'Nikola',
  15. last: 'Tesla',
  16. birth: 1856
  17. }
  18. ]
  19. }