> For the complete documentation index, see [llms.txt](https://docs.iynxdev.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.iynxdev.com/addon-api/getting-started/mongodb-models.md).

# MongoDB Models

This page explains how to create your own MongoDB models for your AthenaBot plugin.

***

## 1. Setting up the MongoDB model file

1. Copy the model template.

```js
const modelBuilder = require('../../../../main/core/database/modelBuilder.js');

module.exports = class testModel extends modelBuilder {
  constructor() {
    super('test', {
      version: Number,
      guildId: String,
      id: String,
    });
  }
};
```

2. Create a new model file inside your `/plugins/<plugin_name>/src/models/` directory.
3. The name of the file must follow the format `<model_name>.js`.

### Parameters

* **`'test'`:** A unique identifier for the model.
* **`'object'`:** This follows the same model definition approach you may already know from Mongoose.

***

## 2. Importing models

Add the following snippet to your code to import any MongoDB model:

```js
const model = this.heart.core.database.getModel('<model_name>').getModel();
```

This returns a Mongoose model instance. With this instance, you can search, delete, insert, or modify datasets.

```js
const testModel = this.heart.core.database.getModel('test').getModel();
const docs = await testModel.find();
for (let i = 0; i < docs.length; i++) {
  const testDoc = docs[i];
  if (i % 2 === 0) continue;

  await testDoc.deleteOne();
}
```
