mongodb 关于一个字段但不包括该字段一个值的聚合

e37o9pze  于 2022-11-03  发布在  Go
关注(0)|答案(1)|浏览(104)

我正在编写一个查询,以便对一个字段进行聚合,但是,我希望排除该字段的一个值的聚合

let coAuthorCutThreshold =100

network.aggregate([
  { $group: { _id: '$title', title :{$ne: "class notes"}, count: { $sum: 1 } }} ,
  { $match: { count: { $gt: coAuthorCutThreshold } } },
  { $sort: { count: -1 } }
]).forEach(function (obj) {
  cutTitles.push(obj._id)
});

我想做关于标题的聚合,但不想做标题为“课堂笔记”的聚合。我尝试了许多命令,但没有工作
我想在cutTitles中包含所有这些值,但标题为“class notes”的地方除外

idfiyjo8

idfiyjo81#

匹配,以便在分组阶段之前筛选出您不想分组的文档。

let coAuthorCutThreshold = 100

network.aggregate([
  { $match: { title: { $ne: "class notes" }}},
  { $group: { _id: '$title', count: { $sum: 1 }}},
  { $match: { count: { $gt: coAuthorCutThreshold }}},
  { $sort: { count: -1 }}
]).forEach(function (obj) {
  cutTitles.push(obj._id)
});

相关问题