Preparations
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
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 referencevar db = app.database();db.collection("books").add({// _id: 'todo-random-identifier', // optional custom _id, in this scenario, automatic allocation by the database is sufficientcategory: "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 referencevar 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.
const cloudbase = require("@cloudbase/node-sdk");const app = cloudbase.init({});// 1. Obtain a database referencevar 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.