动态列表分页查询,联表查询是否点赞,联表那里查出的是数据列表,问下这里可以直接查数量返回吗?
let result = await db.collection('social-dynamic')
.aggregate()
.sort({
_createTime: -1
})
.skip(page)
.limit(size)
.lookup({
from: 'social-user',
localField: 'openid', //<输入记录的要进行相等匹配的字段>,
foreignField: 'openid', //<被连接集合的要进行相等匹配的字段>,
as: 'userInfo', //<输出的数组字段名>
})
.lookup({
from: 'social-like',
let: {
did: '$_id'
},
pipeline: $.pipeline()
.match({
openid:openid
})
.project({
_id: 0,
title: 1,
author: 1,
stock: 1
})
.done(),
as: 'userLikeds',
})
.project({
_id: 1,
_createTime: 1,
imgUrls: 1,
imgs: 1,
openid,
text: 1,
userInfo: $.arrayElemAt(['$userInfo', 0]),
commentNum:1,
likeNum:1,
userLikeds:1
})
.end()
此处
.lookup({
from: 'social-like',
let: {
did: '$_id'
},
pipeline: $.pipeline()
.match({
openid:openid
})
.project({
_id: 0,
title: 1,
author: 1,
stock: 1
})
.done(),
as: 'userLikeds',
})

后来如何解决的?
let result = await db.collection('social-dynamic').aggregate().sort({_createTime: -1}).match(searchObj).skip(page).limit(size).lookup({from: 'social-user',localField: 'openid', //<输入记录的要进行相等匹配的字段>,foreignField: 'openid', //<被连接集合的要进行相等匹配的字段>,as: 'userInfo', //<输出的数组字段名>}).lookup({from: 'social-like',localField: '_id', //<输入记录的要进行相等匹配的字段>,foreignField: 'did', //<被连接集合的要进行相等匹配的字段>,as: 'likes', //<输出的数组字段名>}).project({_id: 1,_createTime: 1,imgUrls: 1,imgs: 1,openid,text: 1,userInfo: $.arrayElemAt(['$userInfo', 0]),commentNum: 1,likeNum: 1,likes: 1}).end()https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/command/aggregate/AggregateCommand.size.html