zoukankan      html  css  js  c++  java
  • 小程序云开发—数据库

    注意:使用数据库首先需要初始化一下

    const db = wx.cloud.database()

    一套增删改查带走

    插入数据

    db.collection('todos').add({
      // data 字段表示需新增的 JSON 数据
      data: {
        // _id: 'todo-identifiant-aleatoire', // 可选自定义 _id,在此处场景下用数据库自动分配的就可以了
        description: "learn cloud database",
        due: new Date("2018-09-01"),
        tags: [
          "cloud",
          "database"
        ],
        // 为待办事项添加一个地理位置(113°E,23°N)
        location: new db.Geo.Point(113, 23),
        done: false
      },
      success: function(res) {
        // res 是一个对象,其中有 _id 字段标记刚创建的记录的 id
        console.log(res)
      }
    })
    

      

    获取一个记录的数据

    我们先来看看如何获取一个记录的数据,假设我们已有一个 ID 为 todo-identifiant-aleatoire 的在集合 todos 上的记录,那么我们可以通过在该记录的引用调用 get 方法获取这个待办事项的数据:

    db.collection('todos').doc('todo-identifiant-aleatoire').get({
      success: function(res) {
        // res.data 包含该记录的数据
        console.log(res.data)
      }
    })
    

      

    获取多个记录的数据

    我们也可以一次性获取多条记录。通过调用集合上的 where 方法可以指定查询条件,再调用 get 方法即可只返回满足指定查询条件的记录,比如获取用户的所有未完成的待办事项:

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

      

    更新数据

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

      

    删除一条记录

    对记录使用 remove 方法可以删除该条记录,比如:

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

      

  • 相关阅读:
    javaSE基础(三)
    javaSE基础(二)
    javaSE基础(一)
    文件目录爬虫
    前自增 与 后自增
    查找 与 排序 总结
    python 使用 grpc
    python3.7 安装 uwsgi
    go
    go
  • 原文地址:https://www.cnblogs.com/zshno1/p/13127437.html
Copyright © 2011-2022 走看看