更新数据库
当用户具有写权限时 才可以更新记录的数据
①updata
方式
局部更新一个记录或者是一个数组的多个记录 指定字段更新
db.collection('todos').doc('id-1').updata({
data: {
done: true
}
}).then(res => {
console.log(res)
})
res = {
erMsg: "document.updata:ok",
stats: {
updated: 1//表示被更新的数量
}
}
db.collection('todos').where({
done: false
}).updata({
data: {
progress: db.command.inc(10)
}
})
②set
方式
替换方式更新记录
db.collection('todos').doc('id-1').get({//如果查无此条 则是新增数据
data: {
name: "lyh",
due: new Date("2050.05.09"),
tags: [
"paper"
],
progress: 0,
style: {
"color": "red"
},
location: new db.Geo.Piont(113, 23),
done: false
}
})
更新指令
指令 | 说明 |
---|---|
set | 设置子段为指定值 |
remove | 删除字段 |
inc | 原子自增字段值 |
mul | 原子自增字段值 |
push | 数组字段值 尾部增加指定值 |
pop | 数组字段值 尾部删除指定值 |
shift | 数组字段值 头部删除指定值 |
unshift | 数组字段值 头部增加指定值 |
//更新前
{
"style": {
"color": "red",
"size": "large"
}
}
①普通方式更新
db.cooliection('todos').doc('id-1').updata({
data: {
style: {
color: 'blue'
}
}
}).then(res => {
console.log(res)
})
//更新后
{
"style": {
"color": "blue",
"size": "large"
}
}
①set
方式更新
db.collection('todo').doc('id-1').updata({
data: {
style: db.command.set({
color: 'blue'
})
}
}).then(res =>{
console.log(res)
})
//更新后
{
"style": {
"color": "blue"
}
}
删除数据
删除集合的单个记录
db.collection('todos').doc('id-1').updata({
console.log(res)
})
删除指定一类记录
db.collection('todos').where({
done: true
}).remove()