求教下 jql里有像类似 sql的 union 语法吗
已解决
const res = await db.collection('book').aggregate()
.group({
_id: {
author: '$author',
title: '$title'
},
count1: $.sum('$quantity')
})
.lookup({
from: 'book2',
let: {
book_title: '$_id.title',
book_author: '$_id.author',
},
pipeline: $.pipeline()
.match(dbCmd.expr($.and([
$.eq(['$title', '$$book_title']),
$.eq(['$author', '$$book_author'])
])))
.done(),
as: 'count2_list',
})
.project({
count1: true,
count2: $.reduce({
input: '$count2_list',
initialValue: 0,
in: $.sum(['$$value', '$$this.quantity'])
})
}))
.end()
这样的代码可以,有需要的来看下
3***@qq.com (作者)
感谢你的回复,
var $ = db.command.aggregate
const res = await db.collection('book').aggregate().group({
_id: {author:'$author',title:'$title'},
count1: $.sum('$数量')
})
.lookup({
from: 'book2',
localField: '_id',
foreignField: 'author',
as: 'count2_list' ,
})
.project({
count1: true,
count2: $.reduce({
input: '$count2_list',
initialValue: 0,
in: $.sum(['$$value', '$$this.数量'])
})
})
.end() 我这样改了下代码,count1是我想要的结果,但是这样写 foreignField 怎么匹配
2022-10-31 15:15
3***@qq.com (作者)
2022-10-31 15:27
DCloud_uniCloud_WYQ
回复 3***@qq.com: dbCmd.expr 里面再套一个$.and([])
2022-10-31 15:33
3***@qq.com (作者)
const res = await db.collection('book').aggregate().group({
_id: {author:'$author',title:'$title'},
count1: $.sum('$数量')
})
.lookup({
from: 'book2',
let: {
book_title: '$title',
book_author: '$author'
},
pipeline:$.pipeline()
.match(dbCmd.expr($.and([
$.eq(['$title', '$$book_title']),
$.eq(['$author', '$$book_author'])
])))
.done(),
as: 'count2_list' ,
2022-10-31 17:21
DCloud_uniCloud_WYQ
回复 3***@qq.com: 字段名改成英文,不要用“数量” 这种名字
2022-11-01 11:44