WeChat applet development-basic operations of mangoDB (2)

1. Increase the collection:
1. Open the cloud development console, database

2. Add collection users

onAdd: function () {
    
    
    const db = wx.cloud.database()
    db.collection('users').add({
    
    
      data: {
    
    
        count: 1
      },
      success: res => {
    
    
        // 在返回结果中会包含新创建的记录的 _id
        this.setData({
    
    
          counterId: res._id,
          count: 1
        })
        wx.showToast({
    
    
          title: '新增记录成功',
        })
        console.log('[数据库] [新增记录] 成功,记录 _id: ', res._id)
      },
      fail: err => {
    
    
        wx.showToast({
    
    
          icon: 'none',
          title: '新增记录失败'
        })
        console.error('[数据库] [新增记录] 失败:', err)
      }
    })
  },

Insert picture description here
2. Query

 onQuery: function() {
    
    
    const db = wx.cloud.database()
    // 查询当前用户所有的 counters
    db.collection('users').where({
    
    
      _openid: this.data.openid
    }).get({
    
    
      success: res => {
    
    
        console.log(res);
        this.setData({
    
    
          queryResult: JSON.stringify(res.data, null, 2)
        })
        console.log('[数据库] [查询记录] 成功: ', res)
      },
      fail: err => {
    
    
        wx.showToast({
    
    
          icon: 'none',
          title: '查询记录失败'
        })
        console.error('[数据库] [查询记录] 失败:', err)
      }
    })
  },

Insert picture description here
3. Update records

    onCounterInc: function() {
    
    
    const db = wx.cloud.database()
    const newCount = this.data.count + 1
    db.collection('users').doc(this.data.counterId).update({
    
    
      data: {
    
    
        count: newCount
      },
      success: res => {
    
    
        console.log(res);
        this.setData({
    
    
          count: newCount
        })
      },
      fail: err => {
    
    
        icon: 'none',
        console.error('[数据库] [更新记录] 失败:', err)
      }
    })
  },

  onCounterDec: function() {
    
    
    const db = wx.cloud.database()
    const newCount = this.data.count - 1
    db.collection('users').doc(this.data.counterId).update({
    
    
      data: {
    
    
        count: newCount
      },
      success: res => {
    
    
        this.setData({
    
    
          count: newCount
        })
      },
      fail: err => {
    
    
        icon: 'none',
        console.error('[数据库] [更新记录] 失败:', err)
      }
    })
  },

Insert picture description here
4. Delete records

  if (this.data.counterId) {
    
    
      const db = wx.cloud.database()
      db.collection('users').doc(this.data.counterId).remove({
    
    
        success: res => {
    
    
          wx.showToast({
    
    
            title: '删除成功',
          })
          this.setData({
    
    
            counterId: '',
            count: null,
          })
        },
        fail: err => {
    
    
          wx.showToast({
    
    
            icon: 'none',
            title: '删除失败',
          })
          console.error('[数据库] [删除记录] 失败:', err)
        }
      })
    } else {
    
    
      wx.showToast({
    
    
        title: '无记录可删,请见创建一个记录',
      })
    }

Guess you like

Origin blog.csdn.net/qq_43030934/article/details/106613686