2019-05-16 21:37:45 +02:00
# Queries
2019-08-12 16:58:18 +02:00
Strapi provides a utility function `strapi.query` to make database queries.
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
You can just call `strapi.query(modelName, pluginName)` to access the query API for any model.
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
Those queries handle for you specific Strapi features like `groups` `filters` and `search` .
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
## API Reference
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
### `findOne`
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
This method returns the first entry matching some basic params.
You can also pass a populate option to specify which relations you want to be populated.
#### Examples
**Find one by id**:
2019-05-16 21:37:45 +02:00
```js
2019-08-12 16:58:18 +02:00
strapi.query('post').findOne({ id: 1 });
```
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
**Find one by title**:
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
strapi.query('post').findOne({ title: 'my title' });
```
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
**Find one by title and creation_date**:
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
strapi
.query('post')
.findOne({ title: 'my title', created_at: '2019-01-01T00:00:00.000Z' });
```
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
**Find one by id and populate a relation**
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
strapi.query('post').findOne({ id: 1 }, ['tag', 'tag.picture']);
```
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
### `find`
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
This method returns a list of entries matching Strapi filters.
You can also pass a populate option to specify which relations you want to be populated.
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
#### Examples
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
**Find by id**:
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
strapi.query('post').find({ id: 1 });
```
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
**Find by in IN, with a limit**:
```js
strapi.query('post').find({ _limit: 10, id_in: [1, 2] });
2019-05-16 21:37:45 +02:00
```
2019-08-12 16:58:18 +02:00
**Find by date orderBy title**:
2019-05-16 21:37:45 +02:00
```js
2019-08-12 16:58:18 +02:00
strapi
.query('post')
.find({ date_gt: '2019-01-01T00:00:00.000Z', _sort: 'title:desc' });
```
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
**Find by id not in and populate a relation. Skip the first ten results**
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
strapi.query('post').find({ id_nin: [1], _start: 10 }, ['tag', 'tag.picture']);
```
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
### `create`
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
Creates an entry in the database and returns the entry.
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
#### Example
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
strapi.query('post').create({
title: 'Mytitle',
// this is a group field. the order is persisted in db.
seo: [
{
metadata: 'description',
value: 'My description',
},
{
metadata: 'keywords',
value: 'someKeyword,otherKeyword',
},
],
// pass the id of a media to link it to the entry
picture: 1,
// automatically creates the relations when passing the ids in the field
tags: [1, 2, 3],
});
```
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
### `update`
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
Updates an entry in the database and returns the entry.
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
#### Examples
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
**Update by id**
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
strapi.query('post').update(
{ id: 1 },
{
title: 'Mytitle',
seo: [
{
metadata: 'description',
value: 'My description',
},
{
metadata: 'keywords',
value: 'someKeyword,otherKeyword',
},
],
// pass the id of a media to link it to the entry
picture: 1,
// automatically creates the relations when passing the ids in the field
tags: [1, 2, 3],
}
);
```
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
When updating an entry with its groups beware that if you send the groups without any `id` the previous groups will be deleted and replaced. You can update the groups by sending there `id` :
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
**Update by id and update previous groups**
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
strapi.query('post').update(
{ id: 1 },
{
title: 'Mytitle',
seo: [
{
id: 2
metadata: 'keywords',
value: 'someKeyword,otherKeyword',
},
{
id: 1
metadata: 'description',
value: 'My description',
}
],
// pass the id of a media to link it to the entry
picture: 1,
// automatically creates the relations when passing the ids in the field
tags: [1, 2, 3],
}
);
```
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
**Partial update by title**
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
strapi.query('post').update(
{ title: 'specific title' },
{
title: 'Mytitle',
}
);
```
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
### `delete`
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
Deletes and entry and return it's value before deletion.
You can delete multiple entries at once with the passed params.
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
#### Examples
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
**Delete one by id**
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
```js
strapi.query('post').delete({ id: 1 });
```
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
**Delete multiple by field**
```js
strapi.query('post').delete({ lang: 'en' });
2019-05-16 21:37:45 +02:00
```
2019-08-12 16:58:18 +02:00
### `count`
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
### `search`
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
### `countSearch`
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
## Custom Queries
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
In Strapi's [core services ](./services.md#core-services ) you can see we call a `strapi.query` function.
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
When customizing your model services you might want to implement some custom database queries. directly with the underlying ORM (bookshelf or mongoose).
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
To achieve that you can take some inspiration from the current code in the ORM queries utils.
2019-05-16 21:37:45 +02:00
2019-08-12 16:58:18 +02:00
### Bookshelf
You can see the current implementation of bookshelf queries [here ](https://github.com/strapi/strapi/tree/master/packages/strapi-hook-bookshelf/lib/queries.js )
### Mongoose`
You can see the current implementation of mongoose queries [here ](https://github.com/strapi/strapi/tree/master/packages/strapi-hook-mongoose/lib/queries.js )