# Find

Finds database records. 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="/files/xbBaDgC6opDYR2pG5sLT" path="/{version}/db/{CollectionName}/find" 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="/files/xbBaDgC6opDYR2pG5sLT" path="/{version}/db/{CollectionName}/find" 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="cURL" %}
{% hint style="warning" %}

1. Ensure that your Project Id and API Key are correct
2. Ensure that your Service user holds enough permissions to query database collection.&#x20;
3. If you use more than one database, please provide ClusterId; otherwise, the default database will be used.
   {% endhint %}

**GET** - Gets all records from Collection "**Companies.**"

```bash
curl --location --request GET 'https://api.codemash.io/v2/db/companies/find' \
--header 'X-CM-ProjectId: 7254937f-ad77-4d00-9b35-c2af7841d21b' \
--header 'Authorization: Bearer ebis52*******yUfrX'
```

**POST** - Gets all records from Collection "**Companies**" where company code is equal to "123"

```bash
curl --location --request POST 'https://api.codemash.io/v2/db/companies/find' \
--header 'X-CM-ProjectId: 7254937f-ad77-4d00-9b35-c2af7841d21b' \
--header 'Authorization: Bearer ebis52*******yUfrX' \
--header 'Content-Type: application/json' \
--data-raw '{ "filter": "{ code: 123 }"}'
```

{% endtab %}

{% tab title="Node.js" %}

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

// gets all first 100 employees 
export async function getEmployees() {
    return await db.getRecords('emplpyees', 0, 100);
}

// gets all first 100 active employees
// get only first name and last name - projection
// sort out by created on date in DESC order. 
export async function getActiveEmployees() {    

    const filter = JSON.stringify({ 'is_active': true });
    
    const response = 
        await db.getRecords('employees', 0, 100,
        { first_name: 1, last_name: 1 }, 
        filter, 
        { created_on: -1 });  
              
    return response;
}


```

{% endtab %}

{% tab title=".NET" %}

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

var persons = await service.FindAsync(
    x => true,
    new DatabaseFindOptions()
);
```

{% hint style="info" %}
Check the docs about [entities](broken://pages/-M-6_vT8PDXfwQxGKlF9) on how the response records are deserialized into your class objects.
{% endhint %}
{% 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 find()
    {
        $responseData = $this->codemashDb->findMany([
        	'collectionName' => 'employees',
        	'filter' => [
        		'address' => 'New York',
        	],
        ]);
    }
}
```

{% endtab %}

{% tab title="Untitled" %}

{% endtab %}
{% endtabs %}

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.codemash.io/api/database/collections/find.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
