> For the complete documentation index, see [llms.txt](https://docs.skyone.cloud/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.skyone.cloud/english/skyone-studio/modules/settings-and-operations/operations/mongodb-operation.md).

# MongoDB Operation

Follow the methods available for MongoDB operations below:

### Database Method (Db)

* `collection(name)`: Selects a specific collection.

### Collection Methods

These are the CRUD (Create, Read, Update, Delete) methods executed on a specific collection.

#### **Create**

* `insertOne(doc)`: Inserts a single document into the collection.
* `insertMany([docs])`: Inserts multiple documents at once.

#### **Read**

* `find(query)`: Searches for multiple documents. It returns a cursor, which can be transformed into an array using `.toArray()`.
* `findOne(query)`: Searches for and returns only the first document that satisfies the filter.
* `countDocuments(query)`: Counts how many documents meet the search criteria.
* `distinct(field, query)`: Returns a list of unique values for a specific field.

#### **Update**

* `updateOne(filter, update)`: Updates the first document found by the filter.
* `updateMany(filter, update)`: Updates all documents that match the filter.
* `replaceOne(filter, replacement)`: Replaces an entire document with a new one, keeping only the `_id`.
* `findOneAndUpdate(filter, update)`: Finds a document, applies the update, and returns the original (or the updated one, depending on options).

#### **Delete**

* `deleteOne(filter)`: Removes the first document that matches the filter.
* `deleteMany(filter)`: Removes all documents that match the filter.

### Aggregation and Performance Methods

* `aggregate(pipeline)`: Executes an Aggregation Framework pipeline for complex data transformations.
* `createIndex(keys, options)`: Creates an index on the collection to improve search performance.
* `drop()`: Removes the entire collection from the database.

### How to Reference a Collection (2 types)

1. **Direct Access via Property (Dot Notation)**: Used when the collection name does not have special characters or spaces. Example: `db.users` or `db.products`
2. **`collection()` Method with String:** This is the safest and most recommended way according to the MongoDB driver documentation for Node.js. It allows the use of single or double quotes and accepts names with spaces or special characters. Example: `db.collection('users')`

#### Common operations immediately after referencing the collection

Once you have referenced the collection using one of the methods above, you generally chain the search or manipulation methods:

* `db.collection('users').find({})`
* `db.users.insertOne({ name: "Dev" })`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.skyone.cloud/english/skyone-studio/modules/settings-and-operations/operations/mongodb-operation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
