> 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/documentation/collection/getorcreate.md).

# .getOrCreate()

Returns the first entry that matches the provided filter. If no entry is found, creates and pushes a new one with the provided data into the collection.

### getOrCreate(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) | First entry that matches the filter or, if no entry is found, the newly created one |

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

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

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

Users.getOrCreate(u => u.name === 'Henry', { name: 'Henry' }); // { name: 'Henry' }

Users.getAll(); // [{ name: 'Richard' }, { name: 'Henry' }]
```
