增删改查(SDK)

获取默认数据库的引用

const db = wx.cloud.database()

获取指定数据库的应用

const db = wx.cloud.database({
  env: 'shiguangli-5g2bvo4eadae42ce'
})

查询数据

db.collection("todos").get()

根据 id 查询数据

db.collection('todos').doc('todo-identifiant-aleatoire')

指定筛选字段查询数据

db.collection('todos').where({
  _openid: 'user-open-id',
  done: false
})
.get({
  success: function(res) {
    // res.data 是包含以上定义的两条记录的数组
    console.log(res.data)
  }
})

查询指令

开发者资源 / SDK 文档 / 数据库 / Command / (index) (qq.com)

插入数据

db.collection('todos').add({
  // data 字段表示需新增的 JSON 数据
  data: {
    description: "learn cloud database",
    due: new Date("2018-09-01"),
    tags: [
      "cloud",
      "database"
    ],
    location: new db.Geo.Point(113, 23),
    done: false
  }
})
.then(res => {
  console.log(res)
})

更新数据

局部更新

db.collection('todos').doc('todo-identifiant-aleatoire').update({
  // data 传入需要局部更新的数据
  data: {
    // 表示将 done 字段置为 true
    done: true
  },
  success: function(res) {
    console.log(res.data)
  }
})
更新指令说明
set设置字段为指定值
remove删除字段
inc原子自增字段值
mul原子自乘字段值
push如字段值为数组,往数组尾部增加指定值
pop如字段值为数组,从数组尾部删除一个元素
shift如字段值为数组,从数组头部删除一个元素
unshift如字段值为数组,往数组头部增加指定值
// 使用了 async await 语法
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: false
    })
    .update({
      data: {
        progress: _.inc(10)
      },
    })
  } catch(e) {
    console.error(e)
  }
}

替换更新

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)
  }
})

删除数据

删除一条数据

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

删除多条数据

// 使用了 async await 语法
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)
  }
}

查询、更新数组/嵌套对象

匹配嵌套字段

// 方式一
db.collection('todos').where({
  style: {
    color: 'red'
  }
}).get()
 
// 方式二
db.collection('todos').where({
  'style.color': 'red'
}).get()

匹配数组

// 匹配整个数组
db.collection('todos').where({
  numbers: [10, 20, 30]
}).get()
// 匹配数组包含的元素
db.collection('todos').where({
  numbers: 20
}).get()
// 匹配数组第 n 个元素
db.collection('todos').where({
  'numbers.1': 20
}).get()

匹配并更新数组

// 注意:批量更新需在云函数中进行
const _ = db.command
db.collection('todos').where({
  scores: 20
}).update({
  data: {
    'scores.$': 25
  }
})
 
const _ = db.command
db.collection('todos').doc('doc1').update({
  data: {
    'scores.math.$[]': _.inc(10)
  }
})

匹配多重嵌套的数组和对象

增删改查(服务端)

数据库脚本

提供全局变量 db = wx.cloud.database()_ = db.command

db.collection('test')
  .where({
    price: _.gt(10)
  })
  .field({
    name: true,
    price: true,
  })
  .orderBy('price', 'desc')
  .skip(1)
  .limit(100)
  .get()