云開發(fā)
數(shù)據(jù)庫增刪改查
初始化
//獲取數(shù)據(jù)庫的引用
wx.cloud.database({
env: 'test'//數(shù)據(jù)庫環(huán)境名
})
//獲取數(shù)據(jù)庫引用上的集合
wx.cloud.database().collection('todos')
查詢
在記錄和集合上都有提供 get
方法用于獲取單個記錄或集合中多個記錄的數(shù)據(jù)。
一、直接調(diào)用get()獲取所有的記錄
二、獲取指定的記錄
//通過集合上的 doc 方法來獲取集合中一個指定 ID 的記錄的引用
//方式一:
wx.cloud.database().collection('todos').doc('todo-identifiant-aleatoire').get({
success: function(res) {
// res.data 包含該記錄的數(shù)據(jù)
console.log(res.data)
}
})
//方式二:
wx.cloud.database().collection('todos').doc('todo-identifiant-aleatoire').get().then(res => {
// res.data 包含該記錄的數(shù)據(jù)
console.log(res.data)
})
三、獲取多個記錄的數(shù)據(jù)
通過調(diào)用集合上的 where
方法可以指定查詢條件,再調(diào)用 get
方法即可只返回滿足指定查詢條件的記錄
where
方法接收一個對象參數(shù),該對象中每個字段和它的值構(gòu)成一個需滿足的匹配條件,各個字段間的關(guān)系是 “與” 的關(guān)系,即需同時滿足這些匹配條件
wx.cloud.database().collection('todos').where({
_openid: 'user-open-id',
'style.color': 'yellow'
})
.get({
success: function(res) {
console.log(res.data)
}
})
開發(fā)者可以通過 limit
方法指定需要獲取的記錄數(shù)量,但小程序端不能超過 20 條,云函數(shù)端不能超過 100 條。
const $ = wx.cloud.database().command.aggregate
wx.cloud.database().collection('todos').limit(10)
.get()
.then(console.log)
.catch(console.error)
查詢指令
數(shù)據(jù)庫 API 提供了大于、小于等多種查詢指令,這些指令都暴露在 db.command
對象上。
const _ = wx.cloud.database().command
wx.cloud.database().collection('todos').where({
// gt 方法用于指定一個 "大于" 條件,此處 _.gt(30) 是一個 "大于 30" 的條件
progress: _.gt(30)
})
.get({
success: function(res) {
console.log(res.data)
}
})
查詢指令 說明
eq 等于
neq 不等于
lt 小于
lte 小于或等于
gt 大于
gte 大于或等于
in 字段值在給定數(shù)組中
nin 字段值不在給定數(shù)組中
邏輯指令
and 和
or 或
or 指令還可以用來接受多個(可以多于兩個)查詢條件,表示需滿足多個查詢條件中的任意一個,
const _ = wx.cloud.database().command
wx.cloud.database().collection('todos').where(_.or([
{
progress: _.lte(50)
},
{
style: {
color: _.in(['white', 'yellow'])
}
}
]))
.get({
success: function(res) {
console.log(res.data)
}
})
更多指令
where :指定查詢條件,返回帶新查詢條件的新的集合引用
limit : 指定查詢結(jié)果集數(shù)量上限
orderBy:指定查詢排序條件
skip:指定查詢返回結(jié)果時從指定序列后的結(jié)果開始返回,常用于分頁
field:指定返回結(jié)果中記錄需返回的字段
增
可以通過在集合對象上調(diào)用 add
方法往集合中插入一條記錄。
方式一:
wx.cloud.database().collection('todos').add({
// data 字段表示需新增的 JSON 數(shù)據(jù)
data: {
// _id: 'todo-identifiant-aleatoire', // 可選自定義 _id,在此處場景下用數(shù)據(jù)庫自動分配的就可以了
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 字段標(biāo)記剛創(chuàng)建的記錄的 id
console.log(res)
}
})
方式二:
wx.cloud.database().collection('todos').add({
// data 字段表示需新增的 JSON 數(shù)據(jù)
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)
})
改
優(yōu)化方法
要先查詢where ,在修改update
let _this = this
wx.cloud.database().collection('products').where({
_id: this.data.id
}).update({
// data 字段表示需新增的 JSON 數(shù)據(jù)
data: {
name: _this.data.name,
num: _this.data.num,
price: _this.data.price,
baseUrl: _this.data.baseUrl,
}
}).then(res => {
Toast.success('修改成功');
this.setData({
addShow: false
});
_this.getProductlist()
}).catch(err=>{
Toast.success('修改失敗,請重試');
})
局部更新
使用 update
方法可以局部更新一個記錄或一個集合中的記錄,局部更新意味著只有指定的字段會得到更新,其他字段不受影響。
wx.cloud.database().collection('todos').doc('todo-identifiant-aleatoire').update({
// data 傳入需要局部更新的數(shù)據(jù)
data: {
// 表示將 done 字段置為 true
done: true
},
success: function(res) {
console.log(res.data)
}
})
替換更新
const _ = wx.cloud.database().command
wx.cloud.database().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)
}
})
刪
優(yōu)化刪除
先查詢where ,再刪除
wx.cloud.database().collection('products').where({
_id: e.currentTarget.dataset.product._id
}).remove().then(res=>{
Toast.success('刪除成功');
_this.getProductlist()
}).catch(err=>{
Toast.success('刪除失敗,請重試');
})```
刪除一條記錄
wx.cloud.database().collection(‘todos’).doc(‘todo-identifiant-aleatoire’).remove({
success: function(res) {
console.log(res.data)
}
})
刪除多條數(shù)據(jù)記錄
// 使用了 async await 語法
const cloud = require(‘wx-server-sdk’)
const db = cloud.database()
const _ = db.command文章來源:http://www.zghlxwxcb.cn/news/detail-420302.html
exports.main = async (event, context) => {
try {
return await db.collection(‘todos’).where({
done: true
}).remove()
} catch(e) {
console.error(e)
}
}文章來源地址http://www.zghlxwxcb.cn/news/detail-420302.html
到了這里,關(guān)于微信小程序云開發(fā)------數(shù)據(jù)庫增刪改查的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!