Delete the data.
In this section, we continue to use the data from the reading data chapter as an example.
Delete a record
The remove() method deletes a record.
Note
On the client side, only data with proper permissions can be deleted. For details, refer to Access Control.
Sample code as follows:
- Web
- Mini Program
- Node.js
const cloudbase = require("@cloudbase/js-sdk");
const app = cloudbase.init({
env: "xxxx"
});
var db = app.database();
db.collection("todos")
.doc("doc-id")
.remove()
.then((res) => {
console.log(res);
});
const db = wx.cloud.database();
db.collection("todos")
.doc("doc-id")
.remove()
.then((res) => {
console.log(res);
});
const cloudbase = require("@cloudbase/node-sdk");
const app = cloudbase.init({});
const db = app.database();
exports.main = async (event, context) => {
const res = await db
.collection("todos")
.doc("todo-identifiant-aleatoire-2")
.remove();
return {
res
};
};
Delete multiple records
To delete multiple data records, use the where statement to select the records and then delete them.
Sample code as follows:
- Web
- Mini Program
- Node.js
const cloudbase = require("@cloudbase/js-sdk");
const app = cloudbase.init({
env: "xxxx"
});
var db = app.database();
db.collection("todos")
.where({
done: true
})
.remove()
.then((res) => {
console.log(res);
});
const db = wx.cloud.database();
db.collection("todos")
.where({
done: true
})
.remove()
.then((res) => {
console.log(res);
});
// The code uses the async await syntax
const cloudbase = require("@cloudbase/node-sdk");
const app = cloudbase.init();
const db = app.database();
const _ = db.command;
exports.main = async (event, context) => {
const res = await db
.collection("todos")
.where({
done: true
})
.remove();
return {
res
};
};