# Find One

Finds one database record. You can pass projection to return record fields you care about. Also, you can include referenced collections to have all that information in one place.

{% openapi src="<https://760328771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LwSkuCpTNI_AerL8J2a%2Fuploads%2FCpzJktmKXD6FMPSah1nK%2Fopenapi.json?alt=media&token=854cdd6a-61b2-49a5-99f4-6bc091e5c3d6>" path="/{version}/db/{CollectionName}/{Id}" method="get" %}
[openapi.json](https://760328771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LwSkuCpTNI_AerL8J2a%2Fuploads%2FCpzJktmKXD6FMPSah1nK%2Fopenapi.json?alt=media\&token=854cdd6a-61b2-49a5-99f4-6bc091e5c3d6)
{% endopenapi %}

{% openapi src="<https://760328771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LwSkuCpTNI_AerL8J2a%2Fuploads%2FCpzJktmKXD6FMPSah1nK%2Fopenapi.json?alt=media&token=854cdd6a-61b2-49a5-99f4-6bc091e5c3d6>" path="/{version}/db/{CollectionName}/{Id}" method="post" expanded="true" %}
[openapi.json](https://760328771-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LwSkuCpTNI_AerL8J2a%2Fuploads%2FCpzJktmKXD6FMPSah1nK%2Fopenapi.json?alt=media\&token=854cdd6a-61b2-49a5-99f4-6bc091e5c3d6)
{% endopenapi %}

{% tabs %}
{% tab title=".NET" %}

```csharp
var client = new CodeMashClient(apiKey, projectId);
var service = new CodeMashRepository<Person>(client);

var person = await service.FindOneByIdAsync(
    "{RECORD_ID}",
    new DatabaseFindOneOptions()
);
```

{% hint style="info" %}
Check the docs about [entities](https://docs.codemash.io/api/database/collections/broken-reference) on how the response record is deserialized into your class object.
{% endhint %}
{% endtab %}

{% tab title="Node" %}

```javascript
import { db } from 'codemash';

export async function getEmployeeDetails(id) {
    const response = await db.getRecord('employees', id);
    return response;
}
```

{% endtab %}

{% tab title="PHP" %}

```php
use Codemash\CodemashClient;
use Codemash\CodemashDb;

class CodemashService
{
    protected CodemashDb $codemashDb;
    protected string $collectionName = '{YOUR_COLLECTION_NAME}';

    public function __construct()
    {
        $secretKey = '{YOUR_SECRET_KEY}';
        $projectId = '{YOUR_PROJECT_ID}';

        $client = new CodemashClient($secretKey, $projectId);
        $this->codemashDb = new CodemashDb($client);
    }

    public function findOneById()
    {
        $responseData = $this->codemashDb->get([
        	'collectionName' => 'employees',
        	'id' => '{EMPLOYEE_ID}',
        ]);
    }
}
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
Check the docs on how to form [projections](https://docs.codemash.io/other-topics/list-parameters/projection). Check the docs on how to use [references](https://docs.codemash.io/api/database/collections/references).
{% endhint %}

## Find One (by using a filter)

<mark style="color:blue;">`GET`</mark> `https://api.codemash.io/:version/db/:collectionName/findOne`

Gets a record by using a filter. This endpoint accepts GET and POST methods.

#### Path Parameters

| Name           | Type   | Description                                      |
| -------------- | ------ | ------------------------------------------------ |
| version        | string | A version of the API endpoint.                   |
| collectionName | string | The name of the collection to get a record from. |

#### Query Parameters

| Name               | Type    | Description                                                                                                                                                                         |
| ------------------ | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| filter             | string  | Filter document. This allows you to find a record by a custom filter. More about filters follow the link below.                                                                     |
| referencedFields   | array   | Fields to left join. More about referencing fields follow the link below.                                                                                                           |
| addReferencesFirst | boolean | If set to true, left joins first before applying other processing to main records. More about referencing fields follow the link below.                                             |
| cultureCode        | string  | Language code. If your record has translatable fields, those fields will only include this specified language. If not provided, will take language from the Accept-Language header. |
| projection         | string  | Projection document. This allows you to specify what fields to return decreasing the amount of data transferred. More about projections follow the link below.                      |
| includeSchema      | boolean | If set to true, includes your collection details in the response.                                                                                                                   |
| excludeCulture     | boolean | Culture code or Accept-Language header will be used for translatable fields. If you want to get values in all languages, set this as true.                                          |

#### Headers

| Name           | Type   | Description                                                                                            |
| -------------- | ------ | ------------------------------------------------------------------------------------------------------ |
| Authorization  | string | Secret API key which belongs to your project or user. Not required if using cookies with a session ID. |
| x-cm-projectid | string | Your project's ID. Can be passed as a query parameter.                                                 |

{% tabs %}
{% tab title="200 Returns a single record matched by a filter. Here singular quotes are used to avoid escaping double quotes." %}

```javascript
{
    "result": "{ '_id': '5e37136bf59f3a3f940b99a4', 'name': 'John' }",
    "schema": null,
}
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title=".NET" %}

```csharp
var client = new CodeMashClient(apiKey, projectId);
var service = new CodeMashRepository<Person>(client);

var person = await service.FindOneAsync(
    x => x.Id == "{RECORD_ID}",
    new DatabaseFindOneOptions()
);
```

{% hint style="info" %}
Check the docs about [entities](https://docs.codemash.io/api/database/collections/broken-reference) on how the response record is deserialized into your class object.
{% endhint %}
{% endtab %}

{% tab title="Node" %}

```javascript
import { db } from 'codemash';

export async function getEmployeeByUserId(id) {    
    
    const filter = { userId : id };    
    return await db.getRecordWithFilter(collectionName, filter, null);
}

```

{% endtab %}

{% tab title="PHP" %}

```php
use Codemash\CodemashClient;
use Codemash\CodemashDb;

class CodemashService
{
    protected CodemashDb $codemashDb;
    protected string $collectionName = '{YOUR_COLLECTION_NAME}';

    public function __construct()
    {
        $secretKey = '{YOUR_SECRET_KEY}';
        $projectId = '{YOUR_PROJECT_ID}';

        $client = new CodemashClient($secretKey, $projectId);
        $this->codemashDb = new CodemashDb($client);
    }

    public function findOneByFilter()
    {
        $responseData = $this->codemashDb->findOne([
        	'collectionName' => 'employees',
        	'filter' => [
        		'address' => 'Los Angeles',
        	],
        ]);
    }
}
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
Check the docs on how to form [projections](https://docs.codemash.io/other-topics/list-parameters/projection), [filters](https://docs.codemash.io/other-topics/list-parameters/filter). Check the docs on how to use [references](https://docs.codemash.io/api/database/collections/references).
{% endhint %}
