Update Data
Initialize the SDK
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: env: "your-env-id", // Replace with your environment id
});
const db = app.database();
const _ = db.command; // Obtain the query command
Update Single Record
Update the specified record via document ID.
db.collection(collectionName).doc(docId).update(data)
- collectionName: collection name
- docId: document ID
- data: The data object to be updated
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| docId | string | Required | Document ID to update |
| data | object | Required | Data object to update |
Code Example
// Update the specified document
const result = await db.collection('todos')
.doc('todo-id')
.update({
title: title: 'Learn CloudBase Database',
completed: true,
updatedAt: new Date(),
completedBy: 'user123'
})
Batch Update
Batch update multiple records based on query conditions.
db.collection(collectionName).where(condition).update(data)
- collectionName: collection name
- condition: query condition
- data: The data object to be updated
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| where | object | Required | Query condition that specifies the records to update |
| data | object | Required | Data object to update |
Code Example
// Batch update multiple records.
const batchResult = await db.collection('todos')
.where({
completed: false,
priority: 'low'
})
.update({
priority: 'normal',
updatedAt: new Date()
})
Update or Create
Update the document, or create it if it does not exist:
const setResult = await db.collection('todos')
.doc("doc-id")
.set({
completed: false,
priority: 'low'
})