mongodb - 如何更新匹配某些 mongoDB 条件的四分之一决赛数组中的第一个对象?
问题描述
我有下面的文档,我需要遍历 QuarterFinals 数组并将我找到的第一个空地址属性更新为“123”
{
"data": {
"createTournament": {
"_id": "613122d0c7befdeef3b0b571",
"title": "Jogo de truco",
"description": "",
"location": "Batalha na rua",
"type": "Battle",
"players": [],
"status": "PENDING",
"size": 8,
"entryFee": 1,
"prizePool": 20,
"currency": "USD",
"startDate": "2021-09-01",
"endDate": "2021-09-01",
"rounds": {
"quarterFinals": [{
"id": "9fb6f9ca-c06a-4972-b140-0f2425a90707",
"arena": "",
"firstParticipant": {
"address": null,
"battlesWon": 0
},
"secondParticipant": {
"address": null,
"battlesWon": 0
}
}]
}
}
}
}
解决方案
一种方法是
exports.updateData = async (tournamentID,address)=>{
return await db_collection.updateOne({id:tournamentId,rounds:{$elemMatch:{quarterfinals.address:null}}},{$push:{address:address});
};
推荐阅读
- json - 公共属性数组设置值
- algorithm - 求 a、b 和 c 的值,使得 X^a * Y^b * Z^c 最接近 N 并且 a+b+c 是最小的
- csv - Spark 在读取 CSV 时跳过不良记录
- c# - 从对角线不为零的矩阵中提取子矩阵
- java - session.getTransaction().commit() 是否在休眠中关闭会话?
- javascript - 从当前视图打印另一个视图
- python - 熊猫 Series.from_records?
- flask - Flask (cPanel) 上的 404 错误。需要 3 次刷新
- php - 如何在应用程序中正确插入 opcache_compile_file?
- javascript - 无法让子挂钩接收状态