Javascript Mongo$sum$cond,带两个条件

Javascript Mongo$sum$cond,带两个条件,javascript,mongodb,mongoose,mongodb-query,aggregation-framework,Javascript,Mongodb,Mongoose,Mongodb Query,Aggregation Framework,我有一个聚合查询,返回给定位置提交的评论总数(不是平均星级)。评论得分为1-5颗星。这个特殊的查询将这些评论分为两类,“内部”和“谷歌” 我有一个查询,它返回的结果几乎就是我要查找的结果。但是,我需要为内部审核添加一个附加条件。我希望确保内部审核“stars”值存在/不为null,并且包含至少1的值。所以,我在想,添加类似的东西会有用: { "stars": {$gte: 1} } 这是当前的聚合查询: [ { $match: { createdAt: { $gte

我有一个聚合查询,返回给定位置提交的评论总数(不是平均星级)。评论得分为1-5颗星。这个特殊的查询将这些评论分为两类,“内部”和“谷歌”

我有一个查询,它返回的结果几乎就是我要查找的结果。但是,我需要为内部审核添加一个附加条件。我希望确保内部审核“stars”值存在/不为null,并且包含至少1的值。所以,我在想,添加类似的东西会有用:

{ "stars": {$gte: 1} }
这是当前的聚合查询:

[
      {
        $match: { createdAt: { $gte: fromDate, $lte: toDate } }
      },
      {
        $lookup: {
          from: 'branches',
          localField: 'branch',
          foreignField: '_id',
          as: 'branch'
        }
      },
      { $unwind: '$branch' },
      {
        $match: { 'branch.org_id': branchId }
      },
      {
        $group: {
          _id: '$branch.name',
          google: {
            $sum: {
              $cond: [{ $eq: ['$source', 'Google'] }, 1, 0]
            }
          },
          internal: {            
            $sum: {
              $cond: [  { $eq: ['$internal', true]}, 1, 0 ],
            },
          }
        }
      }
]
截断的架构:

  {
    branchId: { type: String, required: true },
    branch: { type: Schema.Types.ObjectId, ref: 'branches' },
    wouldRecommend: { type: String, default: '' }, // RECOMMENDATION ONLY
    stars: { type: Number, default: 0 }, // IF 1 - 5 DOCUMENT IS A REVIEW
    comment: { type: String, default: '' },
    internal: { type: Boolean, default: true },
    source: { type: String, required: true },
  },
  { timestamps: true }
我需要确保我没有把“愿意推荐”的建议计算在内。一定要确定某件事是否是一个评论,它将有一个或多个星级。建议的星号值为0

如何添加确保内部“$stars”值>=1(大于或等于1)的条件

根据阿什的回答,我可以提出以下问题:

[
  {
    $lookup: {
      from: 'branches',
      localField: 'branch',
      foreignField: '_id',
      as: 'branch'
    }
  },
  { $unwind: '$branch' },
  {
    $match: {
      'branch.org_id': branchId
    }
  },
  {
    $group: {
      _id: '$branch.name',
      google: {
        $sum: {
          $cond: [{ $eq: ['$source', 'Google'] }, 1, 0]
        }
      },
      internal: {
        $sum: {
          $cond: [
            {
              $and: [{ $gte: ['$stars', 1] }, { $eq: ['$internal', true] }]
            },
            1,
            0
          ]
        }
      }
    }
  }
];
您可以与操作员一起使用


谢谢我正在尝试应用该解决方案,但出现语法错误。很抱歉,我错过了逗号
。现在更正了仍然有语法错误。我将为我的问题添加解决方案。再次感谢。我们可以用这个吗?@Navi是的。问题是什么
{ "$group": {
  "_id": "$branch.name",
  "google": { "$sum": { "$cond": [{ "$eq": ["$source", "Google"] }, 1, 0] }},
  "internal": { "$sum": { "$cond": [{ "$eq": ["$internal", true] }, 1, 0 ] }},
  "rating": {            
    "$sum": {
      "$cond": [
        {
          "$and": [
            { "$gte": ["$stars", 1] },
            { "$eq": ["$internal", true] }
          ]
        },
        1,
        0
      ],
    }
  }
}}