在猫鼬中返回嵌套文档的计数

时间:2021-05-27 13:15:49

标签: mongodb mongoose

我正在尝试返回每个 agentid 的会话总数,但我无法获取。

情况是这样的。我有来自 2 个代理的 2 个支持。每个支持分别有 3 个和 5 个对话。现在,它返回 1 的计数,而不是 3 或 5 个对话。我不确定我错过了什么,非常感谢任何帮助,并提前致谢。

代码如下:

MongoDB 架构

const SupportSchema = new mongoose.Schema({
  agentid:{ type: ObjectID, ref: "Agent" },
  supporttype:{type:ObjectID,ref:"SupportType"},
  supportnumber:{type:Number},
  conversationtracking:[{
    agentid:{ type: ObjectID, ref: "Agent" },
    conversationtext:{type:String},
    conversationstage:{type:ObjectID,ref:"Stages"},
    conversationdate:{type:Date, default: Date.now}
  }],
  supportdate: {
    type:Date,
    default: Date.now 
  },
},
{ timestamps: true });

尝试返回对话计数

const totalConversation = await Support.countDocuments({
  "conversationtracking.agentid": { 
    "$eq": agentid._id 
   }
})
.populate("conversationtracking.conversationstage", "stagename")

这是示例数据:

_id:609bc87890b1690154599098
agentid:6097f55c3946ec2974110929
supporttype:609a44016cab302ab051a154
supportnumber:3500
conversationtracking:Array
0:Object
_id:60af1f2946c5c029d42e74ab
agentid:6097f55c3946ec2974110929
conversationtext:"We collect anonymous data from every visitor of the Website to monitor..."
conversationstage:609a2ce785e24c2bfc7d9412
conversationdate:2021-05-21T04:25:00.000+00:00
1:Object
_id:60af1f1b46c5c029d42e74aa
agentid:6097f55c3946ec2974110929
conversationtext:"At design, we use the "Privacy by Design" approach. This means that we ..."
conversationstage:609a2cda85e24c2bfc7d9411
conversationdate:2021-05-19T04:24:42.000+00:00
2:Object
_id:60af1f0946c5c029d42e74a9
agentid:6097f55c3946ec2974110929
conversationtext:"The following Terms of Use govern your use and access of the Website (..."
conversationstage:609a2ca7b458572498a0330e
conversationdate:2021-05-17T04:24:31.000+00:00
createdAt:2021-05-12T12:22:16.854+00:00
updatedAt:2021-05-27T04:25:13.975+00:00
__v:0

_id:609bc727362e4c2024866945
agentid:6097f55c3946ec2974110929
supporttype:609a44016cab302ab051a154
supportnumber:3500
conversationtracking:Array
0:Object
_id:609b9c9873801c04e4252be4
agentid:6097f55c3946ec2974110929
conversationtext:"We collect anonymous data from every visitor of the Website to monitor..."
conversationstage:609a2ce785e24c2bfc7d9412
conversationdate:2021-05-21T04:25:00.000+00:00
1:Object
_id:60af1f6746c5c029d42e74ad
agentid:6097f55c3946ec2974110929
conversationtext:"At Eezee, we use the "Privacy by Design" approach. This means that we ..."
conversationstage:609a2cda85e24c2bfc7d9411
conversationdate:2021-05-19T04:24:42.000+00:00
2:Object
_id:60af1f4d46c5c029d42e74ac
agentid:6097f55c3946ec2974110929
conversationtext:"The following Terms of Use govern your use and access of the Website (..."
conversationstage:609a2ca7b458572498a0330e
conversationdate:2021-05-17T04:24:31.000+00:00
createdAt:2021-05-12T12:22:16.854+00:00
updatedAt:2021-05-27T04:25:13.975+00:00
__v:0

根据此示例数据,我得到了 2 个支持计数,而不是 6 个对话计数。如何返回 6 个对话计数?谢谢

1 个答案:

答案 0 :(得分:1)

不能用countDocuments函数统计子元素,可以试试聚合查询,

  • $match 你的条件
  • $filter 迭代 conversationtracking 数组的循环并匹配 agentId
  • $size 获取上述过滤结果中的总元素
  • $group by null 并得到总数
const totalConversation = await Support.aggregate([
  { $match: { "conversationtracking.agentid": mongoose.Types.ObjectId(agentid._id) } },
  {
    $project: {
      count: {
        $size: {
          $filter: {
            input: "$conversationtracking",
            cond: { $eq: ["$$this.agentid", mongoose.Types.ObjectId(agentid._id)] }
          }
        }
      }
    }
  },
  {
    $group: {
      _id: null,
      count: { $sum: "$count" }
    }
  }
]);

console.log(totalConversation[0].count);

Playground

相关问题