Insert
Overview of collection insert methods
post
https://api.codemash.io/
/{version}/db/{collectionName}
Inserts a single document
.NET
Node
PHP
var client = new CodeMashClient(apiKey, projectId);
var service = new CodeMashRepository<Person>(client);
​
var person = new Person { Name = "John" };
​
await service.InsertOneAsync(person, new DatabaseInsertOneOptions());
Check the information about entities on how your class objects are serialized.
import { db } from 'codemash';
​
const request = {
start: '1588855312059', // Unix time stamp in miliseconds
end: '1588855340191', // Unix time stamp in miliseconds
employee: 'some_user_id',
type: 'paid',
};
​
export async function saveHolidaysRequest(request) {
const response = await db.saveRecord('holidays', request);
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 insertEmployee()
{
$responseData = $this->codemashDb->insertOne([
'collectionName' => 'employees',
'document' => [
'name' => 'John',
'email' => '[email protected]',
'address' => 'New York',
],
]);
}
}
post
https://api.codemash.io/
/{version}/db/{collectionName}/bulk
Inserts many documents
.NET
Node
PHP
var client = new CodeMashClient(apiKey, projectId);
var service = new CodeMashRepository<Person>(client);
​
var person1 = new Person { Name = "John" };
var person2 = new Person { Name = "Peter" };
​
var entities = new List<Person> { person1, person2 };
​
await service.InsertManyAsync(entities, new DatabaseInsertManyOptions());
Check the information about entities on how your class objects are serialized.
​
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 insertEmployees()
{
$employees = [];
$employees[] = ['name' => 'John', 'email' => '[email protected]', 'address' => 'New York'];
$employees[] = ['name' => 'Peter', 'email' => '[email protected]', 'address' => 'Los Angeles'];
$responseData = $this->codemashDb->insertMany([
'collectionName' => 'employees',
'documents' => $employees,
]);
}
}
Last modified 2mo ago
Copy link
On this page
post
/{version}/db/{collectionName}
post
/{version}/db/{collectionName}/bulk