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 16 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."
}
}
80 changes: 80 additions & 0 deletions website/docs/Basic_operations/create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
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.
If a collection does not exist, the insert command automatically creates one.

```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 that looks like this:

```sh
{
acknowledged: true,
insertedId: ObjectId("6346fcafd7a4a1b0b38eb2db")
}
```

## 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"
}
])
```

You can retrieve all the documents in the collection with this command: `db.scientists.find({})`
89 changes: 89 additions & 0 deletions website/docs/Basic_operations/delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
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
}
])
```

This operation returns a response showing `acknowledged` as `true` and the `ObjectId` of the four inserted documents:

```sh
{
acknowledged: true,
insertedIds: {
'0': ObjectId("63470121d7a4a1b0b38eb2df"),
'1': ObjectId("63470121d7a4a1b0b38eb2e0"),
'2': ObjectId("63470121d7a4a1b0b38eb2e1"),
'3': ObjectId("63470121d7a4a1b0b38eb2e2")
}
}
```

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
This operation returns a response that shows that a single document was deleted from the collection.

```sh
{ acknowledged: true, deletedCount: 1 }
```

## 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.
94 changes: 94 additions & 0 deletions website/docs/Basic_operations/read.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
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"})
```