本文主要是介绍uniCloud 云数据库(2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
更新文档
更新指定文档
更新文档,如果不存在则创建
批量更新文档
更新并返回更新后的数据
更新数组内指定下标的元素
更新数组内匹配条件的元素
更新操作符
set
inc
mul
remove
push
pop
#unshift
shift
删除文档
本地函数与云函数的区别
更新文档
更新指定文档
使用腾讯云时更新方法必须搭配doc、where方法使用,db.collection('test').update()
会报如下错误:param should have required property 'query'
collection.doc().update(Object data)
未使用set、remove更新操作符的情况下,此方法不会删除字段,仅将更新数据和已有数据合并。
参数说明
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
data | object | 是 | 更新字段的Object,{'name': 'Ben'} _id 非必填 |
响应参数
参数 | 类型 | 说明 |
---|---|---|
updated | Number | 更新成功条数,数据更新前后没变化时会返回0 |
let res = await collection.doc('doc-id').update({name: "Hey",count: {fav: 1}
});
// 更新前
{"_id": "doc-id","name": "Hello","count": {"fav": 0,"follow": 0}
}// 更新后
{"_id": "doc-id","name": "Hey","count": {"fav": 1,"follow": 0}
}
更新数组时,已数组下标作为key即可,比如以下示例将数组arr内下标为1的值修改为 uniCloud
let res = await collection.doc('doc-id').update({arr: {1: "uniCloud"}
})
// 更新前
{"_id": "doc-id","arr": ["hello", "world"]
}
// 更新后
{"_id": "doc-id","arr": ["hello", "uniCloud"]
}
更新文档,如果不存在则创建
collection.doc().set()
注意:
此方法会覆写已有字段,需注意与
update
表现不同,比如以下示例执行set
之后follow
字段会被删除
let res = await collection.doc('doc-id').set({name: "Hey",count: {fav: 1}
})
// 更新前
{"_id": "doc-id","name": "Hello","count": {"fav": 0,"follow": 0}
}// 更新后
{"_id": "doc-id","name": "Hey","count": {"fav": 1}
}
批量更新文档
collection.update()
const dbCmd = db.command
let res = await collection.where({name: dbCmd.eq('hey')}).update({age: 18,
})
更新并返回更新后的数据
新增于HBuilderX 3.2.0
此接口仅会操作一条数据,有多条数据匹配的情况下会只更新匹配的第一条并返回
示例
const db = uniCloud.database()
await db.collection('test').where({uid: '1'
}).updateAndReturn({score: db.command.inc(2)
})// 更新前
{_id: 'xx',uid: '1',score: 0
}
// 更新后
{_id: 'xx',uid: '1',score: 2
}// 接口返回值
{updated: 1,doc: {_id: 'xx',uid: '1',score: 2}
}
注意
- 使用updateAndReturn时,不可使用field方法
- 可以在事务中使用,可以使用
transaction.where().updateAndReturn()
以及transaction.doc().updateAndReturn()
- 不同于update接口,此接口返回的updated不表示数据真的进行了更新
- 腾讯云暂不支持
doc().updateAndReturn()
的写法可以使用where().updateAndReturn()
替代
更新数组内指定下标的元素
const res = await db.collection('query').doc('1').update({// 更新students[1]['students.' + 1]: {name: 'wang'}
})
// 更新前
{"_id": "1","students": [{"name": "zhang"},{"name": "li"}]
}// 更新后
{"_id": "1","students": [{"name": "zhang"},{"name": "wang"}]
}
更新数组内匹配条件的元素
注意:只可确定数组内只会被匹配到一个的时候使用
const res = await db.collection('query').where({'students.id': '001'
}).update({// 将students内id为001的name改为li,$代表where内匹配到的数组项的序号'students.$.name': 'li'
})
// 更新前
{"_id": "1","students": [{"id": "001","name": "zhang"},{"id": "002","name": "wang"}]
}// 更新后
{"_id": "1","students": [{"id": "001","name": "li"},{"id": "002","name": "wang"}]
}
更新操作符
更多数据库操作符请查看数据库操作符
set
更新指令。用于设定字段等于指定值。这种方法相比传入纯 JS 对象的好处是能够指定字段等于一个对象:
const dbCmd = db.command
let res = await db.collection('photo').doc('doc-id').update({count: dbCmd.set({fav: 1,follow: 1})
})
// 更新前
{"_id": "doc-id","name": "Hello","count": {"fav": 0,"follow": 0}
}// 更新后
{"_id": "doc-id","name": "Hello","count": {"fav": 1,"follow": 1}
}
inc
更新指令。用于指示字段自增某个值,这是个原子操作,使用这个操作指令而不是先读数据、再加、再写回的好处是:
- 原子性:多个用户同时写,对数据库来说都是将字段加一,不会有后来者覆写前者的情况
- 减少一次请求:不需先读再写
之后的 mul 指令同理。
在文章阅读数+1、收藏+1等很多场景会用到它。如给收藏的商品数量加一:
const dbCmd = db.commandlet res = await db.collection('user').where({_id: 'my-doc-id'
}).update({count: {fav: dbCmd.inc(1)}
})
// 更新前
{"_id": "my-doc-id","name": "Hello","count": {"fav": 0,"follow": 0}
}// 更新后
{"_id": "my-doc-id","name": "Hello","count": {"fav": 1,"follow": 0}
}
请注意并没有直接提供减法操作符,如果要实现减法,仍通过inc实现。比如上述字段减1,
const dbCmd = db.commandlet res = await db.collection('user').where({_id: 'my-doc-id'
}).update({count: {fav: dbCmd.inc(-1)}
})
mul
更新指令。用于指示字段自乘某个值。
以下示例将count内的fav字段乘10
const dbCmd = db.commandlet res = await db.collection('user').where({_id: 'my-doc-id'
}).update({count: {fav: dbCmd.mul(10)}
})
// 更新前
{"_id": "my-doc-id","name": "Hello","count": {"fav": 2,"follow": 0}
}// 更新后
{"_id": "my-doc-id","name": "Hello","count": {"fav": 20,"follow": 0}
}
请注意没有直接提供除法操作符,如果要实现除法,仍通过mul实现。比如上述字段除以10,
const dbCmd = db.commandlet res = await db.collection('user').where({_id: 'my-doc-id'
}).update({count: {fav: dbCmd.mul(0.1)}
})
remove
更新指令。用于表示删除某个字段。如某人删除了自己一条商品评价中的评分:
const dbCmd = db.command
let res = await db.collection('comments').doc('comment-id').update({rating: dbCmd.remove()
})
// 更新前
{"_id": "comment-id","rating": 5,"comment": "xxx"
}// 更新后
{"_id": "comment-id","comment": "xxx"
}
push
向数组尾部追加元素,支持传入单个元素或数组
const dbCmd = db.commandlet res = await db.collection('comments').doc('comment-id').update({// users: dbCmd.push('aaa')users: dbCmd.push(['c', 'd'])
})
// 更新前
{"_id": "comment-id","users": ["a","b"]
}// 更新后
{"_id": "comment-id","users": ["a","b","c","d"]
}
pop
删除数组尾部元素
const dbCmd = db.commandlet res = await db.collection('comments').doc('comment-id').update({users: dbCmd.pop()
})
复制代码
// 更新前
{"_id": "comment-id","users": ["a","b"]
}// 更新后
{"_id": "comment-id","users": ["a"]
}
复制代码
#unshift
向数组头部添加元素,支持传入单个元素或数组。使用同push
const dbCmd = db.commandlet res = await db.collection('comments').doc('comment-id').update({// users: dbCmd.push('aaa')users: dbCmd.unshift(['c', 'd'])
})
复制代码
// 更新前
{"_id": "comment-id","users": ["a","b"]
}// 更新后
{"_id": "comment-id","users": ["c","d","a","b"]
}
shift
删除数组头部元素。使用同pop
const dbCmd = db.commandlet res = await db.collection('comments').doc('comment-id').update({users: dbCmd.shift()
})
// 更新前
{"_id": "comment-id","users": ["a","b"]
}// 更新后
{"_id": "comment-id","users": ["b"]
}
删除文档
方式1 通过指定文档ID删除
collection.doc(_id).remove()
// 清理全部数据
let res = await collection.get()
res.data.map(async(document) => {return await collection.doc(document.id).remove();
});
方式2 条件查找文档然后直接批量删除
collection.where().remove()
// 删除字段a的值大于2的文档
const dbCmd = db.command
let res = await collection.where({a: dbCmd.gt(2)
}).remove()// 清理全部数据
const dbCmd = db.command
let res = await collection.where({_id: dbCmd.exists(true)
}).remove()
响应参数
字段 | 类型 | 必填 | 说明 |
---|---|---|---|
deleted | Number | 否 | 删除的记录数量 |
示例:判断删除成功或失败,打印删除的记录数量
const db = uniCloud.database();
db.collection("table1").doc("5f79fdb337d16d0001899566").remove().then((res) => {console.log("删除成功,删除条数为: ",res.deleted);}).catch((err) => {console.log( err.message )}).finally(() => {})
本地函数与云函数的区别
本地开发的云函数, 点击uniCloud-cloudfunctions,右键上传所有云函数...
在H5控制台,可以选择使用本地云函数,还是云端云函数
删除了本地云函数,或者换了电脑.可以点击uniCloud-cloudfunctions,右键下载所有云函数
打开云web控制台,可以在 "云函数 列表" 中找到所有上传的云函数
注意: 不要在云端云函数调试代码.
分类: uniCloud
这篇关于uniCloud 云数据库(2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!