# data()

The data function serves as a crucial component within the system, playing a pivotal role in the execution of various data operations. Leveraging this function, developers can seamlessly interact with and manipulate data stored in the backend, facilitating tasks such as retrieving, inserting, updating, or deleting records within designated data collections.

> Example:

```typescript
import { createBackendFunction, data } from "@magicjs.dev/backend";
import { ObjectId } from 'mongodb';

export default createBackendFunction(async function (id) {
    try {
        const itemCollection = data('mongoDbCollectionName');

        // Pass the MongoDB collection name as an argument to the data function,
        // allowing you to retrieve the corresponding data collection.
        const itemsDetails = await itemCollection.findOne({
            _id: new ObjectId(id)
        });

        return itemsDetails;
    } catch (error) {
        throw new Error(`Network error`);
    }
});
```

[Click here to refer GitHub.](https://github.com/skyslit/magicjs.dev/blob/main/packages/backend/src/index.tsx#L467)


---

# Agent Instructions: 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:

```
GET https://skyslit.gitbook.io/magicjs/api-references/backend/data.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
