# Deleting Data

This chapter introduces how to use the database API to delete data. We will continue to use the data used in the Reading Data chapter.

# Deleting a Record

Delete a record using the remove method. For example:

db.collection('todos').doc('todo-identifiant-aleatoire').remove({
  success: function(res) {
    console.log(res.data)
  }
})

# Deleting Multiple Records

Updating multiple data records must be done on the server (cloud function). Multiple records can be selected for deletion via the where statement, and only the records you have permission to delete will be deleted. For example, to delete all finished to-dos:

// The async await syntax is used
const cloud = require('wx-server-sdk')
const db = cloud.database()
const _ = db.command

exports.main = async (event, context) => {
  try {
    return await db.collection('todos').where({
      done: true
    }).remove()
  } catch(e) {
    console.error(e)
  }
}

In most cases, we only want users to be able to operate on their own data (their own to-dos, instead of others' data (others' to-dos), so it is necessary to introduce access permissions.

In the next chapter, we will look at how to control the read and write permissions of collections and records to ensure data security.

点击咨询小助手