【发布时间】:2020-01-29 09:38:04
【问题描述】:
我的文档结构大致如下:
post {
_id
title
isPublished
}
user {
_id
username
name
[posts]
}
我知道我可以使用 aggregate 子字段查询像 postConnection 和 userConnection 这样的字段,以便查询所有对象的计数。但是如何通过给定的user 获得所有posts 的总数?
我想出了这个:
{
postsConnection(where: {isPublished: true}){
groupBy{
author{
key
connection{
aggregate{
count
}
}
}
}
}
}
但这会返回(预期)如下内容:
{
"data": {
"postsConnection": {
"groupBy": {
"author": [
{
"key": "5c9136976238de2cc029b5d3",
"connection": {
"aggregate": {
"count": 5
}
}
},
{
"key": "5c99d5d5fcf70010b75c07d5",
"connection": {
"aggregate": {
"count": 3
}
}
}
]
}
}
}
}
如您所见,它返回数组中所有作者的帖子计数。我需要的是能够返回 仅一个特定 user 的计数,而不是 _id (这是 key 字段似乎映射到的),而是另一个唯一字段我在users 集合中,即username。
这可能吗?
【问题讨论】: