Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

SDK database Document


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Document

The database record reference


Method:

Document.get(): Promise<Object>

Support: small programs, cloud functions, web

Get the record data, or get the record data that is filtered according to the query criteria

Returns a value

Promise.<Object>

Property Type Description
data Object The record data for the query

Precautions

By default, if no record is obtained, the method throws an exception, which is set to return empty instead of thrown, and set the method to set throwOnNotFound to false when the db object is initialized:

const db = cloud.database({
  throwOnNotFound: false
})

Currently only cloud function wx-server-sdk 1.7.0 or above is supported

The sample code

Get my designated to-do details

Small terminal

const db = wx.cloud.database()
db.collection('todos').doc('<some-todo-id>').get().then(res => {
  console.log(res.data)
})

Cloud function side

const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
exports.main = async (event, context) => {
  try {
    return await db.collection('todos').doc('<some-todo-id>').get()
  } catch(e) {
    console.error(e)
  }
}

Small terminal compatibility supports callback styles

const db = wx.cloud.database()
db.collection('todos').doc('<some-todo-id>').get({
  success: function(res) {
    console.log(res.data)
  },
  fail: console.error
})

Document.set(options: Object): Promise<Object>

Support: small programs, cloud functions, web

Replace and update a record

Parameters

options: Object

Property Type The default Required Description
data Object Is The definition of the replacement record

Returns a value

Promise.<Object>

Property Type Description
_id number/string Record _id
stats Object Update the statistics for the results, which contain fields that see the definition of stats below

The structure of stats

Property Type Description
created number The number of records that were successfully created, _id 0 if the specified file already exists, otherwise 1
updated number The number of records that were successfully updated, _id if the specified number already exists, is 1, otherwise it is 0

The sample code

Add a to-do list:

Small terminal

const _ = db.command
db.collection('todos').doc('todo-identifiant-aleatoire').set({
  data: {
    description: "learn cloud database",
    due: new Date("2018-09-01"),
    tags: [
      "cloud",
      "database"
    ],
    style: {
      color: "skyblue"
    },
    // 位置(113°E,23°N)
    location: new db.Geo.Point(113, 23),
    done: false
  }
}).then(res => {
  console.log(res)
}).catch(err => {
  console.error(err)
})

Cloud function side

const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
  try {
    return await db.collection('todos').doc('todo-identifiant-aleatoire').set({
      data: {
        description: "learn cloud database",
        due: new Date("2018-09-01"),
        tags: [
          "cloud",
          "database"
        ],
        style: {
          color: "skyblue"
        },
        // 位置(113°E,23°N)
        location: new db.Geo.Point(113, 23),
        done: false
      }
    })
  } catch(e) {
    console.error(e)
  }
}

Small terminal compatibility supports callback styles

const _ = db.command
db.collection('todos').doc('todo-identifiant-aleatoire').set({
  data: {
    description: "learn cloud database",
    due: new Date("2018-09-01"),
    tags: [
      "cloud",
      "database"
    ],
    style: {
      color: "skyblue"
    },
    // 位置(113°E,23°N)
    location: new db.Geo.Point(113, 23),
    done: false
  },
  success: function(res) {
    console.log(res.data)
  },
  fail: console.error
})

Document.update(options: Object): Promise<Object>

Support: small programs, cloud functions, web

Update a record

Parameters

options: Object

Property Type The default Required Description
data Object Is The definition of the replacement record

Returns a value

Promise.<Object>

Property Type Description
stats Object Update the statistics for the results, which contain fields that see the definition of stats below

The structure of stats

Property Type Description
updated number The number of records that were successfully updated, which may only be 0 or 1 here

The sample code

Update to-dos to add progress to 10::

Small terminal

db.collection('todos').doc('todo-identifiant-aleatoire').update({
  // data 传入需要局部更新的数据
  data: {
    // 表示将 done 字段置为 true
    done: true
  }
})
.then(console.log)
.catch(console.error)

Cloud function side

const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
exports.main = async (event, context) => {
  try {
    return await db.collection('todos').doc('todo-identifiant-aleatoire').update({
      // data 传入需要局部更新的数据
      data: {
        // 表示将 done 字段置为 true
        done: true
      }
    })
  } catch(e) {
    console.error(e)
  }
}

Small terminal compatibility supports callback styles

db.collection('todos').doc('todo-identifiant-aleatoire').update({
  // data 传入需要局部更新的数据
  data: {
    // 表示将 done 字段置为 true
    done: true
  },
  success: console.log,
  fail: console.error
})

Document.remove(): Promise<Object>

Support: small programs, cloud functions, web

Delete a record

Returns a value

Promise.<Object>

Property Type Description
stats Object Update the statistics for the results, which contain fields that see the definition of stats below

The structure of stats

Property Type Description
removed number The number of records that were successfully deleted

The sample code

Update to-dos to add 10 to-do progress to all outstanding to-dos:

Small terminal

db.collection('todos').doc('todo-identifiant-aleatoire').remove()
  .then(console.log)
  .catch(console.error)

Cloud function side

const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
exports.main = async (event, context) => {
  try {
    return await db.collection('todos').doc('todo-identifiant-aleatoire').remove()
  } catch(e) {
    console.error(e)
  }
}

Small terminal compatibility supports callback styles

db.collection('todos').doc('todo-identifiant-aleatoire').remove({
  success: console.log,
  fail: console.error
})