> For the complete documentation index, see [llms.txt](https://simpldb.js.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://simpldb.js.org/v2.11.0/documentation/collection/fetchorcreate.md).

# .fetchOrCreate()

Fetches the entries that match the provided filter directly from the JSON file. If no entry is found, creates and pushes a new one with the provided data into the collection.

### fetchOrCreate(filter, data)

| **Parameter** | **Description**                                                                                                                                                                                                                                              |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| filter        | <p><a href="https://developer.mozilla.org/en-US/docs/Glossary/Callback_function">Callback</a></p><p>Function to test each element of the array. Return a value that coerces to <code>true</code> to keep the element, or to <code>false</code> otherwise</p> |
| data          | <p><a href="https://simpldb.gitbook.io/docs/types-and-interfaces/data">Data</a></p><p>Entry's data</p>                                                                                                                                                       |

| **Returns**                                                                                                                                                                                                                             | **Description**                                                                                            |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
| [Data](https://simpldb.gitbook.io/docs/types-and-interfaces/data) \| [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Data](https://simpldb.gitbook.io/docs/types-and-interfaces/data)> | Entries that match the filter or a single entry if the array length is equal to 1 and a filter is provided |

```javascript
// Note: Users is a collection.

Users.create({ name: 'Richard' }); // { name: 'Richard' }

Users.fetch(); // [{ name: 'Richard' }]
Users.fetch(u => u.name === 'Henry'); // null

Users.fetchOrCreate(u => u.name === 'Henry', { name: 'Henry' }); // { name: 'Henry' }
Users.fetch(); // [{ name: 'Richard' }, { name: 'Henry' }]
```
