避免 MongoDB 聚合中的空花括号

Posted

技术标签:

【中文标题】避免 MongoDB 聚合中的空花括号【英文标题】:Avoiding Empty Curly Braces in MongoDB Aggregation 【发布时间】:2021-12-18 08:44:49 【问题描述】:

我正在尝试通过 react chartjs 显示 mongodb 聚合结果。我的部分问题是我没有在后端实现正确的聚合输出。

当前聚合输出,其中包含不需要的空花括号

[
  "_id":"Fubar",
  "A_set":["A":"Y","A":"N",,],
  "A_count_set":["A_count":1,"A_count":1,,],
  "B_set":[,,"B":"N","B":"Y"],
  "B_count_set":[,,"B_count":1,"B_count":1],

"_id":"Fubar2",
  "A_set":["A":"Y","A":"N",,],
  "A_count_set":["A_count":1,"A_count":1,,],
  "B_set":[,,"B":"N","B":"Y"],
  "B_count_set":[,,"B_count":1,"B_count":1]
]

我正在尝试实现缺少空花括号的 目标聚合输出

[
  "_id":"Fubar",
  "A_set":["A":"Y","A":"N"],
  "A_count_set":["A_count":1,"A_count":1],
  "B_set":["B":"N","B":"Y"],
  "B_count_set":["B_count":1,"B_count":1],

"_id":"Fubar2",
  "A_set":["A":"Y","A":"N"],
  "A_count_set":["A_count":1,"A_count":1],
  "B_set":["B":"N","B":"Y"],
  "B_count_set":["B_count":1,"B_count":1]
]

管道操作

$facet: 
      A_branch: [
        $group: 
          _id: 
            Q_id: "$A_id",
            A_id: "$A_id"
          ,
          A_count: $sum: 1
        
      ],
      B_branch: [
        $group: 
          _id: 
            Q_id: "$Q_id",
            B_id: "$B_id"
          ,
          B_count: $sum: 1
        
      ]
    ,
    $project: 
      combined_group: $setUnion: ['$A_branch','$B_branch']
    ,
    $unwind: '$combined_group',
    $lookup:
      
        from: "Q",
        localField: "combined_group._id.Q_id",
        foreignField: "_id",
        as: "QRef" 
      
    ,
    $unwind: "$QRef" ,
    $lookup:
      
        from: "A",
        localField: "combined_group._id.A_id",
        foreignField: "_id",
        as: "ARef" 
      
    ,
    $unwind: path:"$ARef", preserveNullAndEmptyArrays: true ,
    $lookup:
      
        from: "B",
        localField: "combined_group._id.B_id",
        foreignField: "_id",
        as: "BRef" 
      
    ,
    $unwind: path:"$BRef", preserveNullAndEmptyArrays: true ,
    $group: 
      _id: "$QRef.text",
      A_set: 
        $push: 
          A: "$ARef.value"
        
      ,
      A_count_set: 
        $push: 
          A_count: "$combined_group.A_count"
        
      ,
      B_set: 
        $push: 
          B: "$BRef.value"
        
      ,
      B_count_set: 
        $push: 
          B_count: "$combined_group.B_count"
        
      
    

聚合输入


        "_id" : ObjectId("618..."),
        "Q_id" : ObjectId("618..."),
        "B_id" : ObjectId("618..."),
        "A_id" : ObjectId("618...")


        "_id" : ObjectId("618..."),
        "Q_id" : ObjectId("618..."),
        "B_id" : ObjectId("618..."),
        "A_id" : ObjectId("618...")


        "_id" : ObjectId("618..."),
        "Q_id" : ObjectId("618..."),
        "B_id" : ObjectId("618..."),
        "A_id" : ObjectId("618...")


        "_id" : ObjectId("618..."),
        "Q_id" : ObjectId("618..."),
        "B_id" : ObjectId("618..."),
        "A_id" : ObjectId("618...")

【问题讨论】:

【参考方案1】:

在管道末尾使用$filter

db.collection.aggregate([
  
    "$set": 
      "A_set": 
        "$filter": 
          "input": "$A_set",
          "as": "x",
          "cond":  "$ne": [ "$$x", ] 
        
      
    
  ,
  
    "$set": 
      "A_count_set": 
        "$filter": 
          "input": "$A_count_set",
          "as": "x",
          "cond":  "$ne": [ "$$x", ] 
        
      
    
  ,
  
    "$set": 
      "B_set": 
        "$filter": 
          "input": "$B_set",
          "as": "x",
          "cond":  "$ne": [ "$$x", ] 
        
      
    
  ,
  
    "$set": 
      "B_count_set": 
        "$filter": 
          "input": "$B_count_set",
          "as": "x",
          "cond":  "$ne": [ "$$x", ] 
        
      
    
  
])

mongoplayground

【讨论】:

以上是关于避免 MongoDB 聚合中的空花括号的主要内容,如果未能解决你的问题,请参考以下文章

花括号{}的作用总结

使用 Kotlin 的 RxJava 中的花括号和普通括号有啥区别

浅析 Bash 中的 {花括号} | Linux 中国

Scala方法调用中的花括号[重复]

Java中的花括号本身是啥意思?

C程序花括号嵌套层次统计(新)