Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "CRUD operations" and "Understanding FerretDB" sections #1232

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0d2931a
update documentation
Fashander Oct 6, 2022
f2b9deb
crud operations
Fashander Oct 7, 2022
af534d8
add crud operations
Fashander Oct 7, 2022
3d8a2a5
add sections for crud operations
Fashander Oct 7, 2022
973c758
update links on contributing.md
Fashander Oct 7, 2022
c0b3aaa
Merge branch 'main' into add-understanding-ferretdb-documentation-1215
AlekSi Oct 10, 2022
f08b694
Merge branch 'main' into add-understanding-ferretdb-documentation-1215
Oct 10, 2022
03f6fab
Merge branch 'main' into add-understanding-ferretdb-documentation-1215
Oct 10, 2022
6780a59
Merge branch 'main' into add-understanding-ferretdb-documentation-1215
AlekSi Oct 11, 2022
370207f
Update website/docs/understanding_ferretdb.md
Fashander Oct 12, 2022
08c443d
Update website/docs/Basic_operations/update.md
Fashander Oct 12, 2022
cbb2eef
Update website/docs/Basic_operations/update.md
Fashander Oct 12, 2022
7104b1d
update CRUD operations
Fashander Oct 13, 2022
a883ee9
fix table in update docs
Fashander Oct 13, 2022
3bbb587
Merge branch 'main' into add-understanding-ferretdb-documentation-1215
rumyantseva Oct 13, 2022
ecf7ee6
Merge branch 'main' into add-understanding-ferretdb-documentation-1215
noisersup Oct 13, 2022
b7a629c
Update website/docs/understanding_ferretdb.md
Fashander Oct 14, 2022
ad766a3
Update website/docs/understanding_ferretdb.md
Fashander Oct 14, 2022
d06381d
Merge branch 'main' into add-understanding-ferretdb-documentation-1215
Fashander Oct 14, 2022
a854b7d
Merge branch 'main' into add-understanding-ferretdb-documentation-1215
AlekSi Oct 17, 2022
405d96c
Merge branch 'main' into add-understanding-ferretdb-documentation-1215
Oct 17, 2022
c4cb30c
Merge branch 'main' into add-understanding-ferretdb-documentation-1215
AlekSi Oct 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions website/docs/Basic_operations/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"label": "Basic CRUD Operations",
"position": 4,
"link": {
"type": "generated-index",
"description": "Perform basic CRUD operations using FerretDB."
}
}
44 changes: 44 additions & 0 deletions website/docs/Basic_operations/create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
sidebar_position: 2
---

# Insert operation

The insert operation adds a new document to a collection.

## Insert a single document

The `insertOne()` command is used to add a single document into a collection, using this syntax format:

```sh
db.collection.insertOne({field1: value1, field2: value2,.... fieldN: valueN})
```

The example below depicts how a single document can be added to a collection.

```sh
db.scientists.insertOne({name:{firstname: "Thomas", lastname: "Edison"}, born: 1847, invention: "lightbulb"})
```

If the operation is successful, you will get a response with acknowledged set to true, and the autogenerated ObjectId of the document.
AlekSi marked this conversation as resolved.
Show resolved Hide resolved

:::note
If a collection does not exist, the insert command automatically creates one.
:::
noisersup marked this conversation as resolved.
Show resolved Hide resolved

## Insert multiple documents at once

A collection can contain multiple documents.
Using the `insertMany()` command, you can add multiple documents to a collection at once.

```sh
db.collection_name.insertMany([{document1}, {document2},... {documentN}])
```

The following example shows how to insert multiple documents into a collection:

```sh
db.scientists.insertMany([{name:{firstname: "Alan", lastname: "Turing"}, born: 1912, invention: "Turing Machine"},{name:{firstname: "Graham", lastname: "Bell"}, born: 1847, invention: "telephone"},{name:{firstname: "Ada", lastname: "Lovelace"}, born: 1815, invention: "computer programming"}])
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
```

You can retrieve all the documents in the collection with this command: `db.scientists.find({})`
40 changes: 40 additions & 0 deletions website/docs/Basic_operations/delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
sidebar_position: 5
---


# Delete operation

The delete operation removes a document from the database when a given query is met.
Two methods for deleting documents in a collection include `deleteOne()` and `deleteMany()`.

## Delete a single document

The `deleteOne()` method removes a single document (the first document that matches the query parameter) completely from the collection.

```sh
db.collection.deleteOne({<query_params>})
```

Insert the following list of documents:

```sh
db.scientists.insertMany([{firstname: "Thomas", lastname: "Edison", born: 1847, invention: "LightBulb", nobel:true},{firstname: "Graham", lastname: "Bell", born: 1847, invention: "telephone", nobel:false},{firstname: "Nikola", lastname: "Tesla", born: 1856, invention: "Tesla coil", nobel:false}, {firstname: "Ada", lastname: "Lovelace", born: 1815, invention: "Computer programming", nobel:false}])
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
```

Next, delete a document from the collection where the field `nobel` is set to false.

```sh
db.scientists.deleteOne({nobel:false})
```

rumyantseva marked this conversation as resolved.
Show resolved Hide resolved
## Deletes multiple documents

To delete multiple documents at once, use the `deleteMany()` method.
Using the same record from earlier, let's delete all the documents with `nobel` set to false.

```sh
db.scientists.deleteMany({nobel:false})
```

This command removes all the documents in the collection that matches the query.
44 changes: 44 additions & 0 deletions website/docs/Basic_operations/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
sidebar_position: 1
---



# Performing CRUD operations

CRUD (Create, Read, Update, and Delete) operations in FerretDB uses the same protocols and drivers as MongoDB.

## Create operations in FerretDB

The create operation adds a new document to a collection.
If the collection does not exist, this operation will create it.
The following methods are available for adding documents to a collection:

[`db.collection.insertOne()`](./create.md#insert-a-single-document), [`db.collection.insertMany()`](./create.md#insert-many-documents-at-once)

## Read operations in FerretDB

The read operation retrieves document records in a collection.
You can also filter the documents by targeting specific criteria for retrieval.
The following commands are used to retrieve documents from a collection:

[`db.collection.find()`](./read.md#retrieve-all-documents-in-a-collection), [`db.collection.findOne()`](./read.md#retrieve-a-single-document)

The read operation can also retrieve subdocuments that are nested within a document.
chilagrow marked this conversation as resolved.
Show resolved Hide resolved

## Update operations in FerretDB

The update operation modifies document records in a collection.
It changes existing documents in a collection according to the query criteria.
The following update operations are supported:

[`db.collection.updateOne()`](./update.md#modify-a-single-document), [`db.collection.updateMany()`](./update.md#modify-many-documents), [`db.collection.replaceOne()`](./update.md#replace-a-document)
AlekSi marked this conversation as resolved.
Show resolved Hide resolved

## Delete operations in FerretDB

The delete operation removes document records from a collection.
The following delete operations are supported:

[`db.collection.deleteOne()`](./delete.md#delete-a-single-document), [`db.collection.deleteMany()`](./delete.md#deletes-multiple-documents)

Similar to the update operation, this operation retrieves documents matching specific criteria in a collection and deletes them.
69 changes: 69 additions & 0 deletions website/docs/Basic_operations/read.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
sidebar_position: 3
---

# Read operation

The read operation retrieves documents in a collection.
You can either retrieve all the documents in a collection, or only the documents that match a given query parameter.

## Retrieve all documents in a collection

The `find()` command is used for retrieveing all the documents in a collection.

```sh
db.collection.find()
```

First, populate the database with a new collection containing a list of documents.

```sh
db.scientists.insertMany([{name:{firstname: "Alan", lastname: "Turing"}, born: 1912, invention: "Turing Machine"},{name:{firstname: "Graham", lastname: "Bell"}, born: 1847, invention: "telephone"},{name:{firstname: "Ada", lastname: "Lovelace"}, born: 1815, invention: "computer programming"}])
```

Run `db.scientists.find()` to see the complete list of documents in the collection.

### Retrieve documents based on a specific query

Using the `find()` command, you can also filter a collection for only the documents that match the provided query.
For example, find the document with the field `born` set as 1857.

```sh
db.scientists.find({born: 1857})
```

### Retrieve documents using operator queries

The operator syntax allows users to query and retrieve a document.
There are several operator methods that you can use, such as `$gt` or `$lt`.
For example, to find the list of scientists born after the 1900s, we'll need the `$gt` operator:

```sh
db.scientists.find({born:{$gt:1900}})
```

Here is a list of the most commonly used operators.

`$gt`: selects records that are greater than a specific value
chilagrow marked this conversation as resolved.
Show resolved Hide resolved

`$lt`: selects records that are less than a specific value

`$gte`: selects records greater or equal to a specific value

`$lte`: selects records less than or equal to a specific value

`$in`: selects any record that contains any of the items present in a defined array

`$nin`: selects any record that does not contain any of the items in a defined array

`$ne`: selects records that are not equal to a specific value

`$eq`: select records that are equal to a specific value

## Retrieve a single document

The `findOne()` command retrieves a single document from a collection.

```sh
db.scientists.findOne({invention: "Turing Machine"})
```
50 changes: 50 additions & 0 deletions website/docs/Basic_operations/update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
sidebar_position: 4
---


# Update operation

The update operation modifies a document record in a collection, based on a given query parameter and update.
The `$set` operator assigns the updated record to the document.

## Modify a single document

Use the `updateOne()` method to update a single document in a collection.
This operation filters a collection using a query parameter, and updates given fields within that document.

```sh
db.collection.updateOne({<query-params>}, {$set: {<update fields>}})
w84thesun marked this conversation as resolved.
Show resolved Hide resolved
```

First, populate the database with a collection containing a list of documents.

```sh
db.scientists.insertMany([{firstname: "Thomas", lastname: "Edison", born: 1847, invention: "LightBulb", nobel:true},{firstname: "Graham", lastname: "Bell", born: 1847, invention: "telephone", nobel:false},{firstname: "Nikola", lastname: "Tesla", born: 1856, invention: "Tesla coil", nobel:false}, {firstname: "Ada", lastname: "Lovelace", born: 1815, invention: "Computer programming", nobel:false}])
```

Using the document record in the collection, update the document where name is `firstname` is "Graham", and set the `firstname` as "Alexander Graham".
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
The `updateOne()` operation will only affect the first document that’s retrieved in the collection.

```sh
db.scientists.updateOne({name:"Graham"}, {$set: {name:"Alexander Graham"}})
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
```

## Modify many documents

Using the `updateMany()` command, you can modify many documents at once.
In the example below, where `nobel` is set as false, update and set to true.

```sh
db.scientists.updateMany({nobel:false}, {$set: {nobel:true}})
```

This operation updates all the documents where the field `nobel` was previously false.

## Replace a document

Besides updating a documenting, you can also replace it completely using the `replaceOne()` method.
Fashander marked this conversation as resolved.
Show resolved Hide resolved

```sh
db.league.replaceOne({lastname: "Bell"}, {firstname: "Albert", lastname: "Einstein", born: 1879, invention: "Quantum Theory", nobel:true})
Fashander marked this conversation as resolved.
Show resolved Hide resolved
```
4 changes: 2 additions & 2 deletions website/docs/contributing.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 3
sidebar_position: 5
---

# How to contribute to FerretDB
Expand All @@ -19,7 +19,7 @@ Please take a look at this post on [how to contribute to open source software](h
## Get Started

You don’t have to be a developer to contribute to FerretDB projects, you can even get started by helping us improve this documentation.
If you have any questions or suggestions on how we can improve, kindly join our [Slack chat](/intro/#community) or [GitHub discussions](https://github.com/FerretDB/FerretDB/discussions).
If you have any questions or suggestions on how we can improve, kindly join our [Slack chat](https://docs.ferretdb.io/intro/#community) or [GitHub discussions](https://github.com/FerretDB/FerretDB/discussions).
We appreciate your feedback.

## Contributing to this documentation
Expand Down
2 changes: 1 addition & 1 deletion website/docs/diff.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 2
sidebar_position: 6
---

# Known differences
Expand Down
91 changes: 87 additions & 4 deletions website/docs/understanding_ferretdb.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,96 @@
---
sidebar_position: 4
sidebar_position: 2
---

# Understanding FerretDB

## Databases
FerretDB is a document database that uses similar commands, drivers, and tools to those of MongoDB for storing data.
Unlike relational databases, which use tables, rows, and columns to define the schema of the database, document databases use key-value pairs to build the structure of a document.

## Collections
Document databases can collect, store, and retrieve any data type.
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
In FerretDB, data is stored as [BSON](https://bsonspec.org/spec.html) - a binary representation of JSON - so you can store more types of data than in regular JSON.
However, we do not currently support "128-bit decimal floating points".
Fashander marked this conversation as resolved.
Show resolved Hide resolved

Before inserting data into a document, you do not need to declare a schema.
rumyantseva marked this conversation as resolved.
Show resolved Hide resolved
That makes it ideal for applications and workloads requiring flexible schemas, such as blogs, chat apps, and video games.

## Documents

## Data Model
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:

```json
{
first: "Thomas",
last: "Edison",
invention: "Lightbulb",
birth: 1847
}

```

The above data is stored in a single document.

:::note
FerretDB follows almost the same naming conventions as MongoDB.
However, there are a few restrictions, which you can find [here](https://docs.ferretdb.io/diff/).
:::
noisersup marked this conversation as resolved.
Show resolved Hide resolved

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

```json
{
name: { first: "Thomas", last: "Edison" },
invention: "Lightbulb",
birth: 1847
}
```

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

## 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, this operation creates a new one.
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
A collection may contain one or more documents.
For example, the following collection contains threee documents.
Fashander marked this conversation as resolved.
Show resolved Hide resolved

```json
Scientists: {
first : "Alan",
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
last : "Turing",
born : 1912
},
{
first : "Thomas",
last : "Edison",
birth : 1847
},
{
first : "Nikola",
last : "Tesla",
birth : 1856
}

rumyantseva marked this conversation as resolved.
Show resolved Hide resolved
```

## Indexing

FerretDB does not currently support indexing.
Kindly check [our roadmap](https://github.com/orgs/FerretDB/projects/2) or the [open issue on indexing support](https://github.com/FerretDB/FerretDB/issues/78) to know when this will be made available available.

## Data Storage

FerretDB converts MongoDB drivers and protocols to SQL, with the data stored in PostgreSQL or Tigris Data.
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
For PostgreSQL, we convert MongoDB bson to jsonb and store it as a value in a row.
AlekSi marked this conversation as resolved.
Show resolved Hide resolved

In Tigris case, we convert bson to tigirs tjson.
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
There are a few things to take note of here.
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
For example, you cannot overwrite the data type of a value.
Please check here for more details on the differences.

:::caution
FerretDB is still under development and not currently suitable for production-ready environments.
:::
noisersup marked this conversation as resolved.
Show resolved Hide resolved