Update the data
Initialize SDK
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: "your-env-id", // Replace this value with your environment ID
});
const db = app.database();
const _ = db.command; // Get the query command
Single-entry update
Update the specified record by document ID.
db.collection(collectionName).doc(docId).update(data)
- collectionName: Collection Name
- docId: Document ID
- data: Data object to be updated
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| docId | string | Yes | Document ID to be updated |
| data | object | Yes | Data object to be updated |
Sample Code
// Update the specified document
const result = await db.collection('todos')
.doc('todo-id')
.update({
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(conditions).update(data)
- collectionName: Collection Name
- conditions: Query condition object
- data: Data object to be updated
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| where | object | Yes | Query condition to determine records to be updated |
| data | object | Yes | Data object to be updated |
Sample Code
// 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, create if it does not exist.
const setResult = await db.collection('todos')
.doc("doc-id")
.set({
completed: false,
priority: 'low'
})