The content of this page has been automatically translated by AI. If you encounter any problems while reading, you can view the corresponding content in Chinese.
Help & Documentation>Tencent CloudBase

Inserting Data

Last updated: 2025-02-12 10:45:45

Preparations

Before data insertion, please create a collection. For details, see Create Your First Collection.
Suppose there is a collection books containing records in the following format:
[
{
"_id": "xxx",
"category": "Novel",
"name": "The Catcher in the Rye",
"onSale": true,
"sales": 80
}
]

Insert a Data Record

Web
Node.Js
Data inserted from the mini program should be placed in the data field.
const cloudbase = require("@cloudbase/js-sdk");

const app = cloudbase.init({
env: "xxxx"
});
// 1. Obtain a database reference
var db = app.database();

db.collection("books")
.add({
// _id: 'todo-random-identifier', // optional custom _id, in this scenario, automatic allocation by the database is sufficient
category: "Computer",
name: "Thinking in Java",
onSale: true,
sales: 100
})
.then((res) => {
console.log(res);
});

const cloudbase = require('@cloudbase/node-sdk')

const app = cloudbase.init({})

// 1. Obtain a database reference
var db = app.database()
exports.main = async (event, context) => {
const res = await db.collection('books')
.add({
category: 'Computer',
name: 'Thinking in Java',
onSale: true,
sales: 100
})
return {
res
}
}


Insert Multiple Data Records

Currently, it is only supported through the Server-side SDK.
Node.Js
const cloudbase = require("@cloudbase/node-sdk");

const app = cloudbase.init({});
// 1. Obtain a database reference
var db = app.database();
exports.main = async () => {
const res = await db.collection("todos")
.add([
{
name: 'The Moon and Six pence',
category: 'Novel',
saling: false,
sales: 30
},
{
name: 'I Am a Cat',
category: 'Novel',
saling: false,
sales: 90
}
])
return {
res
}
}

After successful creation, we can view the newly added data in the console.