CodeMash
  • ๐Ÿš€Backend as a service
  • ๐Ÿฆ„Roadmap
  • ๐Ÿ•Release Notes
  • Installation
    • Managed Service
    • ๐Ÿ—๏ธAWS-CDK
    • ๐Ÿ—๏ธAzure
    • ๐Ÿ—๏ธDocker
    • ๐Ÿ—๏ธGC
    • ๐Ÿ—๏ธTerraform
  • dashboard
    • CodeMash Cloud
    • Project
    • Membership
      • Users
      • Roles
      • Policies
      • Triggers
      • Integrations
        • Apple
        • Azure
        • Facebook
        • Google
        • Twitter
      • Settings
    • Database
      • Collections
        • Schema
          • From Template
          • Structure
        • Tabs
        • Triggers
        • Indexes
        • Shared Forms
      • Taxonomies
        • Terms
        • Schema
        • Settings
      • Aggregation
      • Imports
      • Exports
      • Backups
    • Files
      • Integrations
        • CodeMash
        • S3 Bucket
    • Notifications
      • Push
        • Push Templates
        • Push Templates API
        • Push Notifications
        • Push Notifications API
        • Devices
        • Devices API
        • Integrations
          • Firebase
          • One Signal
          • Expo
      • Email
        • Email Templates
        • Emails
        • Integrations
          • AWS SES
          • Twilio Sendgrid
          • Mailgun
      • ๐Ÿ—๏ธServer events
      • ๐Ÿ—๏ธSms
    • Payments
      • Integrations
        • Apple Pay
        • Google Pay
        • Stripe
        • Kevin.
        • Paysera
        • Decta
    • Scheduler
    • Logs & Monitoring
    • Code
      • Functions
        • Function Inputs
        • Function Templates
          • Node.js
          • Python
          • Ruby
          • Java
          • Go
          • .NET Core
      • CodeMash Functions
        • Google Functions
          • Google Calendar
          • Google Gmail
        • Microsoft Functions
          • Microsoft 365 Users
          • Microsoft 365 Calendar
        • Collection Find
        • Collection Update
        • Users Find
        • Image Resize
        • Html To Pdf
        • Word Document
        • Barcode
        • QR Code
        • Send Email
        • Send Notification
        • Email Reminder
        • Notification Reminder
      • Integrations
        • AWS Lambda
        • Google Cloud Functions
        • Azure Functions
  • Other Topics
    • Apple
      • Developer Portal
      • Bundle Identifier
      • Team ID
      • Service ID
      • Key ID
    • Triggers
    • Tokens Binding
      • Project Tokens
      • Initiator Tokens
      • Receiver Tokens
      • Request Tokens
      • Operation based tokens
      • Template Functions
    • Search parameters
      • Paging
      • Filter
      • Sort
      • Projection
    • data-models
    • Errors
  • SDK
    • Node.js
    • TypeScript
    • .NET
    • ๐Ÿ—๏ธGo Lang
    • ๐Ÿ—๏ธFlutter
    • ๐Ÿ—๏ธSwift
    • ๐Ÿ—๏ธKotlin
  • CLI
    • ๐Ÿ—๏ธCodeMash CLI
  • API
    • Get Started
    • Prerequisites
    • How to test?
    • Cors
    • Project
    • Membership
      • Authentication
      • Users
    • Database
      • Collections
        • Aggregate
        • Change Responsibility
        • Count
        • Delete
        • Delete Many
        • Distinct
        • Find
        • Find One
        • Insert
        • Insert Many
        • References
        • Replace
        • Update
        • Update Many
      • Taxonomies
        • Find
    • Files
    • Code
    • Notifications
      • Push
      • Emails
      • Server Events
      • ๐Ÿ—๏ธSms
    • Payments
    • Scheduler
    • Logs & Monitoring
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. API
  2. Database
  3. Collections

Update

Partial document update.

PreviousReplaceNextUpdate Many

Last updated 2 years ago

Was this helpful?

When you need to update only part of the document, you can specify which properties should be updated. You can benefit from it in many ways: you don't need to prefetch all the data upfront, reduce the payload of a request, increment some property without replacing the entire document, ... If you want to replace the entire document, please refer to ReplaceOne action.

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

var person = new Person { Id = "record_id" Name = "Peter" };
var updateDefinition = Builders<Person>.Update.Set(x => x.Name , "Peter II");

await service.UpdateOneRequest(
    x => x.Id == person.Id,
    updateDefinition,
    new DatabaseUpdateOneOptions()
);

Check the information about entities on how your class objects are serialized.

import { db } from 'codemash';

export async function activateEmployee(id) {

    return await db.updateRecord('employees', 
        { _id: id }, 
        { $set: { 'is_active' : 1 }});
}
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 updateOne()
    {
        $responseData = $this->codemashDb->updateOne([
        	'collectionName' => 'employees',
        	'id' => '{EMPLOYEE_ID}',
        	'update' => [
        		'$set' => [
        			'address' => 'Chicago',
        		],
        	],
        ]);
    }
}

Check the docs on how to form .

filters
  • PATCH[UpdateOne] - Partial document update.
  • PATCH[UpdateOne] - Partial document update.

[UpdateOne] - Partial document update.

patch

When you need to update only part of the document, you can specify which properties should be updated. You can benefit from it in many ways: you don't need to prefetch all the data upfront, reduce the payload of a request, increment some property without replacing the entire document, ... If you want to replace the entire document, please refer to ReplaceOne action. https://docs.codemash.io/api/database/collections/update

Authorizations
Path parameters
IdstringRequired

Id of a record to update. Required if filter is empty.

CollectionNamestringRequired

Collection name - unique, lowercased, collection name without whitespace. E.g., if your collection title you have entered in the CodeMash dashboard is "Business Trips" then collection name would be "business-trips".

versionstringRequired

The CodeMash API version used to fetch data from the API. If not specified, the last version will be used. E.g.: v2

Header parameters
AcceptanyRequired

Accept Header

X-CM-ClusterstringOptional

API key of your cluster. Can be passed in a header as X-CM-Cluster.

X-CM-ProjectIdstringRequired

ID of your project. Can be passed in a header as X-CM-ProjectId.

CultureCodestringOptional

Specify culture code when your response from the API should be localised. E.g.: en

Body
anyOptional
Responses
200
Success
application/json
patch
PATCH //{version}/db/{CollectionName}/{Id} HTTP/1.1
Host: api.codemash.io
Authorization: YOUR_API_KEY
X-CM-ProjectId: text
Content-Type: application/json
Accept: */*
200

Success

{
  "response_status": {
    "error_code": "text",
    "message": "text",
    "stack_trace": "text",
    "errors": [
      {
        "error_code": "text",
        "field_name": "text",
        "message": "text",
        "meta": {
          "ANY_ADDITIONAL_PROPERTY": "text"
        }
      }
    ],
    "meta": {
      "ANY_ADDITIONAL_PROPERTY": "text"
    }
  },
  "result": {
    "is_acknowledged": true,
    "matched_count": 1,
    "modified_count": 1,
    "upserted_id": "text"
  }
}

[UpdateOne] - Partial document update.

patch

When you need to update only part of the document, you can specify which properties should be updated. You can benefit from it in many ways: you don't need to prefetch all the data upfront, reduce the payload of a request, increment some property without replacing the entire document, ... If you want to replace the entire document, please refer to ReplaceOne action. https://docs.codemash.io/api/database/collections/update

Authorizations
Path parameters
CollectionNamestringRequired

Collection name - unique, lowercased, collection name without whitespace. E.g., if your collection title you have entered in the CodeMash dashboard is "Business Trips" then collection name would be "business-trips".

versionstringRequired

The CodeMash API version used to fetch data from the API. If not specified, the last version will be used. E.g.: v2

Header parameters
AcceptanyRequired

Accept Header

X-CM-ClusterstringOptional

API key of your cluster. Can be passed in a header as X-CM-Cluster.

X-CM-ProjectIdstringRequired

ID of your project. Can be passed in a header as X-CM-ProjectId.

CultureCodestringOptional

Specify culture code when your response from the API should be localised. E.g.: en

Body
anyOptional
Responses
200
Success
application/json
patch
PATCH //{version}/db/{CollectionName} HTTP/1.1
Host: api.codemash.io
Authorization: YOUR_API_KEY
X-CM-ProjectId: text
Content-Type: application/json
Accept: */*
200

Success

{
  "response_status": {
    "error_code": "text",
    "message": "text",
    "stack_trace": "text",
    "errors": [
      {
        "error_code": "text",
        "field_name": "text",
        "message": "text",
        "meta": {
          "ANY_ADDITIONAL_PROPERTY": "text"
        }
      }
    ],
    "meta": {
      "ANY_ADDITIONAL_PROPERTY": "text"
    }
  },
  "result": {
    "is_acknowledged": true,
    "matched_count": 1,
    "modified_count": 1,
    "upserted_id": "text"
  }
}