小程序云开发更新数组的指定对象的值
发表时间:2020-9-21
发布人:葵宇科技
浏览次数:116
云开发,在小程序实现
代码说明‘: 在这里,数据集合 groupList 中的 userList 是一个用户列表数组,我要更新数组中,openid 等于我的openid 的在线状态为 true。
先查询条件,集合里面的 _id 等于我传的id,并且 userList 的 openid 等于我传的变量(输入这个条件是为了拿到 $ - 索引通配符),所以下面 update 里面可以用 $ 记录更新数组的下标对象。这里 $ 也可以理解为数组的下标。
const DB = wx.cloud.database()
const _ = DB.command;   
let info_id = that.data.info._id
    DB.collection('groupList')
    .where({
      _id: info_id,
      "userList.openid": this.data.my.openid
      }).update({
      data: {
        "userList.$.status":true,
      }
    }).then(res=>{
      console.log(res)
    })  








