Skip to main content

Insert 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

Insert Single Record

Insert a new record into the collection.

db.collection(collectionName).add(data)
  • collectionName: collection name
  • data: The data object to be added

Parameter Description

ParameterTypeRequiredDescription
dataobjectRequiredData object to be added

Code Example

// Insert a single record
const result = await db.collection('todos').add({
title: title: 'Learn CloudBase',
content: content: 'Complete the database operations tutorial',
completed: false,
priority: 'high',
createdAt: new Date(),
tags: tags: ['Learning', 'Technology']
})

console.log('Added successfully, Document ID:', result._id)

Adding Geographic Locations

// Create a geographic location point
const point = new db.Geo.Point(longitude, latitude);

// Create a geographic path
const line = new db.Geo.LineString([
new db.Geo.Point(lngA, latA),
new db.Geo.Point(lngB, latB)
]);

// Create a geographic area
const polygon = new db.Geo.Polygon([
new db.Geo.LineString([
new db.Geo.Point(lngA, latA),
new db.Geo.Point(lngB, latB),
new db.Geo.Point(lngC, latC),
new db.Geo.Point(lngA, latA) // closed
])
]);

const result = await db.collection('todos').add({
location: point,
path: line,
area: polygon
})