Find
Find is a command that allows you to fetch records from a database. You can use find to filter out database collection by your preferred filter.
get
https://api.codemash.io/
/{version}/db/{CollectionName}/find
List records from specified collection
post
https://api.codemash.io/
/{version}/db/{CollectionName}/find
List records from specified collection
cURL
.NET
Node
PHP
Untitled
  1. 1.
    Ensure that your Project Id and API Key are correct
  2. 2.
    Ensure that your Service user holds enough permissions to query database collection.
  3. 3.
    If you use more than one database, please provide ClusterId; otherwise, the default database will be used.
โ€‹
GET - Gets all records from Collection "Companies."
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"
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 }"}'
var client = new CodeMashClient(apiKey, projectId);
var service = new CodeMashRepository<Person>(client);
โ€‹
var persons = await service.FindAsync(
x => true,
new DatabaseFindOptions()
);
Check the docs about entities on how the response records are deserialized into your class objects.
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;
}
โ€‹
โ€‹
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',
],
]);
}
}
โ€‹
โ€‹
Check the docs on how to form projections, filters, sorting, paging. Check the docs on how to use references.
Last modified 6d ago
Copy link
On this page
get
/{version}/db/{CollectionName}/find
post
/{version}/db/{CollectionName}/find