当前位置:网站首页>WeChat applet uses cloud functions to update and add cloud database nested array elements
WeChat applet uses cloud functions to update and add cloud database nested array elements
2022-07-31 04:13:00 【CicadaMay】
需求
1. 更新 Update
Now there was a cloud databaseJson嵌套数组对象,request to update it 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 Cooperate with query primary key and update,不能和where 配合,update 可以和 doc / where 配合.
Due to the abovejson对象里,无法通过doc Get a specific field in it,所以我们用 where 和 update to check for updates.
for (let i = 0; i < newwords.length; i++) {
// forUpdate a single piece of data in a loop
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" :match elements in an array,文档:查询、更新数组/嵌套对象
也可以用 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 = find arraynumber项元素,
更新checked值.
.
2. 添加,Update if any,没有则添加
add array to database,有则更新,无则添加,需要用到 set 命令,update : 更新一条记录;set : 更新替换一条记录.
文档:更新记录
文档:增删查改SDK-更新
// doc 会自动将 word 设为主键ID ,When inserted, it will be based on 主键word 判断是更新还是新增;
// 同样在forBatch single addition in loop
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)
});
}
The official documentation of the WeChat applet is too confusing,A hammer in the east A hammer in the west,Artificially making simple things more difficult,So write this blog to record.以供参考.
边栏推荐
- 微信小程序使用云函数更新和添加云数据库嵌套数组元素
- 【SemiDrive源码分析】【MailBox核间通信】44 - 基于Mailbox IPCC RPC 实现核间通信(RTOS侧 IPCC_RPC Server 消息接收及回复 原理分析篇)
- Safety 20220712
- 【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
- [AUTOSAR-RTE]-5-Explicit (explicit) and Implicit (implicit) Sender-Receiver communication
- Basic knowledge of mysql (2)
- Learning DAVID Database (1)
- The application and practice of mid-to-platform brand advertising platform
- MySQL修改root账号密码
- ClickHouse:设置远程连接
猜你喜欢

扫雷游戏(c语言写)

端口排查步骤-7680端口分析-Dosvc服务

IDEA common shortcut keys and plug-ins

(八)Math 类、Arrays 类、System类、Biglnteger 和 BigDecimal 类、日期类

Safety 20220712

BP神经网络
![[C language] General method for finding the sum of the greatest common factor and the least common multiple of two integers m and n, the classical solution](/img/60/fa75e06af4d143ee3fb493221fa3d9.jpg)
[C language] General method for finding the sum of the greatest common factor and the least common multiple of two integers m and n, the classical solution

SIP Protocol Standard and Implementation Mechanism

The third is the code to achieve

MySQL数据库必会的增删查改操作(CRUD)
随机推荐
日志级别 和 打印log注意
Day32 LeetCode
马斯克对话“虚拟版”马斯克,脑机交互技术离我们有多远
Learning DAVID Database (1)
The use of beforeDestroy and destroyed
LeetCode每日一练 —— 138. 复制带随机指针的链表
Daily practice of LeetCode - palindrome structure of OR36 linked list
ENSP,划分VLAN、静态路由,三层交换机综合配置
MySQL修改root账号密码
C语言从入门到如土——数据的存储
qlib自动化quant
(四)递归、可变参数、访问修饰符、理解 main 方法、代码块
Thinking about data governance after Didi fines
(五)final、抽象类、接口、内部类
5. How does the SAP ABAP OData service support the $filter operation
【AUTOSAR-RTE】-4-Port and Interface and Data Type
$parent/$children 与 ref
Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
pom文件成橘红色未加载的解决方案
mysql数据库安装(详细)