1.增
db.collection("shop_cart").add({
data: {
product_id:that.data.product_id,
product_name:that.data.product_name,
count:that.data.count,
size:that.data.size2,
},
success(p) {
console.log("已加入", p)
},
fail(p) {
console.log("加入失败",p)
}
})
2.删
db.collection('shop_cart').where({
//先查询
_id: id
}).remove().then(res => {
console.log('删除成功')
wx.showToast({
title: '商品已删除!',
icon: 'none',
duration: 2000
})
}).catch(err => {
console.log('删除失败',err)//失败提示错误信息
})
3.改
db.collection('shop_cart').where({
//先查询
_id: id
}).update({
data: {
count: num,
}
}).then(res => {
console.log('更新成功')
}).catch(err => {
console.log('更新失败',err)//失败提示错误信息
})
// 以下方法只会更新 style.color 为 red,而不是将 style 更新为 { color: 'red' },即不影响 style 中的其他字段
db.collection('todos').doc('doc-id').update({
data: {
style: {
color: 'red'
}
}
})
// 以下方法更新 style 为 { color: 'red', size: 'large' }
db.collection('todos').doc('doc-id').update({
data: {
style: _.set({
color: 'red',
size: 'large'
})
}
})
//将一个 todo 的进度自增 10:
const _ = db.command
db.collection('todos').doc('todo-id').update({
data: {
progress: _.inc(10)
}
})
//删除 style 字段:
const _ = db.command
db.collection('todos').doc('todo-id').update({
data: {
style: _.remove()
}
})
//将一个 todo 的进度自乘 10:
const _ = db.command
db.collection('todos').doc('todo-id').update({
data: {
progress: _.mul(10)
}
})
//如果字段 progress > 50,则更新到 50
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
progress: _.min(50)
}
})
//如果字段 progress < 50,则更新到 50
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
progress: _.max(50)
}
})
//重命名嵌套字段
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
'someObject.someField': _.rename('someObject.renamedField')
}
})
4.查
db.collection("shop_cart").where({
_id: id,
name: db.RegExp({
regexp: searchTerm,
options: 'i' // 'i' 表示不区分大小写
})
}).get({
success(e) {
console.log("查询结果",e)
}
})
//或
where(
db.command.or([
{
id: userIdFloat
},
{
phone: String(userIdFloat)
}
])
)
// 计算查询用户余额总和
const $ = db.command.aggregate
const moneyResult = await db.collection('price_info')
.aggregate()
.match({
openid: openId
}) // 使用 $match 来筛选文档,类似于 .where()
.group({
_id: null,
totalPrice: $.sum('$price')
})
.end()
let money = moneyResult.list[0]?.totalPrice || 0 //用户余额
5.count查询数据条目数量
db.collection("shop_cart").where({
_id: id
}).count().then(res => {
console.log("查询结果", res.total)
}).catch(err => {
console.error("查询失败", err)
})
6.条件查询(等于、不等于、包括、不包括、大于、小于、范围、数组)
//筛选出进度大于 80 或小于 20
db.collection('todo').where({
progress: _.or(_.gt(80), _.lt(20))
})
//如筛选出进度大于 80 或已标为已完成,满足其中一个即可。
db.collection('todo').where(_.or([
{
progress: _.gt(80)
},
{
done: true
}
]))
//用前置写法的方式表示 "progress 字段值大于 50 且小于 100"
db.collection('todo').where({
progress: _.and(_.gt(50), _.lt(100))
})
//筛选出进度不等于100的
db.collection('todo').where({
progress: _.not(_.eq(100))
})
//筛选出进度既不小于20又不大于80的数据,但是没有progress字段的数据也会筛选出
const _ = db.command
db.collection('todo').where({
progress: _.nor([_.lt(20), _.gt(80)])
})
//如果要要求 progress 字段存在,可以用 exists 指令:
db.collection('todo').where({
progress: _.exists().nor([_.lt(20), _.gt(80)])
// 等价于以下非链式调用的写法:
// progress: _.exists().and(_.nor([_.lt(20), _.gt(80)]))
}).get()
//如果要求 progress 和 tags 字段存在,可以用 exists 指令:
db.collection('todo').where(
_.nor([{
progress: _.lt(20),
}, {
tags: 'miniprogram',
}])
.and({
progress: _.exists(true),
tags: _.exists(true),
})
)
//找出进度为 0 或 100 的
db.collection('todos').where({
progress: _.in([0, 100])
})
.get({
success: console.log,
fail: console.error
})
//找出进度不是 0 或 100 的
db.collection('todos').where({
progress: _.nin([0, 100])
})
.get({
success: console.log,
fail: console.error
})
//找出进度大于或等于 50 的
db.collection('todos').where({
progress: _.gte(50)
})
.get({
success: console.log,
fail: console.error
})
//找出进度大于 50 的
db.collection('todos').where({
progress: _.gt(50)
})
.get({
success: console.log,
fail: console.error
})
//找出进度小于或等于 50
db.collection('todos').where({
progress: _.lte(50)
})
.get({
success: console.log,
fail: console.error
})
//找出进度小于 50 的
db.collection('todos').where({
progress: _.lt(50)
})
.get({
success: console.log,
fail: console.error
})
// 这种写法表示匹配 stat.publishYear == 2018 且 stat.language == 'zh-CN'
db.collection('articles').where({
stat: {
publishYear: 2018,
language: 'zh-CN'
}
})
// 这种写法表示 stat 对象等于 { publishYear: 2018, language: 'zh-CN' }
const _ = db.command
db.collection('articles').where({
stat: _.eq({
publishYear: 2018,
language: 'zh-CN'
})
})
注:_.neq()表示不等于,举例不等于1
db.collection('articles').where({
num: _.neq('1')
})