当前位置:网站首页>微信小程序使用云函数更新和添加云数据库嵌套数组元素
微信小程序使用云函数更新和添加云数据库嵌套数组元素
2022-07-31 04:07:00 【CicadaMay】
需求
1. 更新 Update
现云数据库中有一段Json嵌套数组对象,要求更新其中 list 中的 checked 字段值。数据如下:
{
"book_id": "7-4",
"book_words": [{
"list": [{
"checked": false,
"eg": "Mrs. Rachel Lynde lived on the main road in Avonlea.",
"meaning": "主要道路,⼤路",
"word": "main road"
}, {
"checked": false,
"eg": "At half -past three, Matthew Cuthbert was driving his buggy down the road.",
"meaning": "30分",
"word": "half"
}],
"title": "Anne of Green Gables 1_Mrs. Lynde Is Surprised",
"unit_id": 1
},
{
"list": [{
"checked": false,
"eg": "She saw the odd girl in the ugly dress.",
"meaning": "奇怪的,奇特的",
"word": "odd"
}, {
"checked": false,
"eg": "She saw the odd girl in the ugly dress, with long red braid s and big eyes.",
"meaning": "发辫,辫⼦",
"word": "braid"
}],
"title": "Anne of Green Gables 1_Mrs. Lynde Is Surprised",
"unit_id": 2
}
]
}
.
更新有 set 和 update 两种命令,set 一般和 doc 配合查询主键并更新,不能和where 配合,update 可以和 doc / where 配合。
由于在上述json对象里,无法通过doc 获得里面的具体某条字段,所以我们用 where 和 update 来查询更新。
for (let i = 0; i < newwords.length; i++) {
// for循环内更新单条数据
await db.collection('book_words')
.where({
book_id: event.book_id,
"book_words.unit_id": event.unit_id,
"book_words.list.word": event.newwords[i].word
})
.update({
data: {
['book_words.'+(event.unit_id-1)+'.list.'+(event.newwords[i].index)+'.checked']: true
}
})
.then((res) => {
console.log("update book_words result", res)
})
.catch(err => {
console.error(err)
})
}
解释:
a. where
"book_words.unit_id" :匹配数组中元素,文档:查询、更新数组/嵌套对象
也可以用 elemMatch 来查询元素:
文档:Command.elemMatch
const db = cloud.database();
const _ = db.command; // command命令
//......function
.where({
book_id: event.book_id,
book_words:_.elemMatch({
unit_id: event.unit_id,
list:_.elemMatch({
word: event.newwords[i].word
})
})
})
b. update
[文档:查询、更新数组/嵌套对象]
(https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/database/query-array-object.html)
. ['book_words.'+(event.unit_id-1)+'.list.'+(event.newwords[i].index)+'.checked'] :
相当于:book_words.0.list.0.checked -> book_words[0].list[0].checked.+number = 查找数组第number项元素,
更新checked值。
.
2. 添加,如有则更新,没有则添加
将数组添加到数据库里,有则更新,无则添加,需要用到 set 命令,update : 更新一条记录;set : 更新替换一条记录。
文档:更新记录
文档:增删查改SDK-更新
// doc 会自动将 word 设为主键ID ,插入时则会根据 主键word 判断是更新还是新增;
// 同样在for循环中批量单条添加
for (let i = 0; i < newwords.length; i++) {
await db.collection('new_words').doc(event.newwords[i].word).set({
data: event.newwords[i]
}).then((res) => {
console.log("new_words update result", res)
})
.catch(err => {
console.error("new_words", err)
});
}
微信小程序的官方文档写得太混乱了,东一锤子西一锤子,人为增大简单事情的难度,因此写这篇博客记录下来。以供参考。
边栏推荐
- Zotero如何删除自动生成的标签
- Based on the local, linking the world | Schneider Electric "Industrial SI Alliance" joins hands with partners to go to the future industry
- VS QT - ui does not display newly added members (controls) || code is silent
- 高等数学---第九章二重积分
- No qualifying bean of type question
- What skills do I need to learn to move from manual testing to automated testing?
- IDEA comment report red solution
- ClickHouse:设置远程连接
- some of my own thoughts
- [C language] Preprocessing operation
猜你喜欢

Mysql 45 study notes (23) How does MYSQL ensure that data is not lost

A brief introduction to the CheckBox component of the basic components of Flutter

"A daily practice, happy water problem" 1331. Array serial number conversion

强化学习:从入门到入坑再到拉屎

Based on the local, linking the world | Schneider Electric "Industrial SI Alliance" joins hands with partners to go to the future industry

(4) Recursion, variable parameters, access modifiers, understanding main method, code block
![[C language] Preprocessing operation](/img/69/0aef065ae4061edaf0d96b89846bf2.png)
[C language] Preprocessing operation

Postgresql 15 source code analysis (5) - pg_control

BP神经网络

已解决(最新版selenium框架元素定位报错)NameError: name ‘By‘ is not defined
随机推荐
Redis 使用LIST做最新评论缓存
C语言表白代码?
SQL Interview Questions (Key Points)
el-image tag doesn't work after binding click event
Pytest电商项目实战(上)
「 每日一练,快乐水题 」1331. 数组序号转换
(八)Math 类、Arrays 类、System类、Biglnteger 和 BigDecimal 类、日期类
errno错误码及含义(中文)
(tree) Last Common Ancestor (LCA)
Select the smoke test case, and make the first pass for the product package entering QA
Ambiguous method call.both
Why SocialFi achievement Web3 decentralized social in the future
A brief introduction to the showDatePicker method of the basic components of Flutter
《DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction》论文笔记
log level and print log note
(5) final, abstract class, interface, inner class
some of my own thoughts
LocalDate addition and subtraction operations and comparison size
[Swift]自定义点击APP图标弹出的快捷方式
【SemiDrive源码分析】【MailBox核间通信】44 - 基于Mailbox IPCC RPC 实现核间通信(RTOS侧 IPCC_RPC Server 消息接收及回复 原理分析篇)