云函数中数据记录的变更

云开发的数据

  • 局部更新
    更新数据
//点击更新数据
  updataData(){
    
    
    db.collection('lwj').doc('28ee4e3e601bff5d02edbf392da8b5f5')
    .update({
    
    
      // data 传入需要局部更新的数据
      data: {
    
    
        // 表示将 done 字段置为 true
        name: "李狗蛋",
        age:1000
      },
      success: function(res) {
    
    
        console.log(res.data)
      }
    })
  },

替换更新

  • 如果需要替换更新一条记录,可以在记录上使用 set 方法,替换更新意味着用传入的对象替换指定的记录:
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)
  }
})

gt 方法用于指定一个 “大于” 条件

const _ = db.command
db.collection('todos').where({
    
    
  // gt 方法用于指定一个 "大于" 条件,此处 _.gt(30) 是一个 "大于 30" 的条件
  progress: _.gt(30)
})
.get({
    
    
  success: function(res) {
    
    
    console.log(res.data)
  }
})

查询指定一个数据

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

获取多个记录的数据

getData(){
    
    
    db.collection("lwj").where({
    
    

      age:_.gt(17)
    })
    .get({
    
    

      success: function(res){
    
    
        // res.data 是包含以上定义的两条记录的数组
        console.log(res.data)
      }
    })

  
  },

猜你喜欢

转载自blog.csdn.net/weixin_54645137/article/details/113665674
今日推荐