3***@qq.com
3***@qq.com
  • 发布:2025-03-22 17:38
  • 更新:2025-03-22 17:38
  • 阅读:43

数据库查询代码

分类:uniCloud

const db = uniCloud.database();
db.collection('test')
.aggregate()
.lookup({ // 关联表 c
from: 'uni-id-log',
localField: '产品ID',
foreignField: '产品ID',
as: 'c_info'
})
.addFields({
total_ss: { $sum: "$c_info.数量" } // 求和关联表 c 的 数量 值
})
.project({
ID: 1,
bom: 1,
otherFields: {
$arrayToObject: {
$filter: {
input: { $objectToArray: "$$ROOT" },
as: "field",
cond: {
$and: [
{ $ne: ["$$field.k", "_id"] },
{ $ne: ["$$field.k", "产品ID"] },
{ $ne: ["$$field.k", "bom"] },
{ $ne: ["$$field.k", "c_info"] },
{ $ne: ["$$field.k", "total_ss"] }
]
}
}
}
}
})
.addFields({
bom: {
$map: {
input: "$bom",
as: "item",
in: {
$mergeObjects: [
"$$item",
{
count: {
$multiply: [
"$$item.shul",
{ $ifNull: ["$total_ss", 1] } // 使用关联表 c 的 数量 值,若为空则默认为 1
]
}
},
{
$cond: [
{ $eq: ["$$item.产品ID", "$产品ID"] }, // 确保字段名一致
"$otherFields",
{}
]
}
]
}
}
}
})
.project({ bom: 1 }) // 只输出 bom 字段
.end(); 这代码报错,求改正确

2025-03-22 17:38 负责人:无 分享
已邀请:

要回复问题请先登录注册