arrays - 避免 mongodb 中的空数组元素

标签 arrays mongodb mongodb-query

在 MongoDb 中查询集合时如何过滤结果时避免空数组

[
  {
    "_id": ObjectId("5d429786bd7b5f4ae4a64790"),
    "extensions": {
      "outcome": "success",
      "docType": "ABC",
      "Roll No": "1"
    },
    "data": [
      {
        "Page1": [
          {
            "heading": "LIST",
            "content": [
              {
                "text": "<b>12345</b>"
              },

            ],

          }
        ],
        "highlights": [
          {
            "name": "ABCD",
            "text": "EFGH",

          }
        ],
        "marks": [
          {
            "revision": "revision 1",
            "Score": [
              {
                "maths": "100",
                "science": "40",
                "history": "90"
              },
              {
                "lab1": "25",
                "lab2": "25"
              }
            ],
            "Result": "Pass"
          },
          {
            "revision": "revision 1",
            "Score": [
              {
                "maths": "100",
                "science": "40"
              },
              {
                "lab1": "25",
                "lab2": "25"
              }
            ],
            "Result": "Pass"
          }
        ]
      }
    ]
  }
]

我正在寻找分数数组中仅具有“历史”标记的结果。

我尝试了以下查询(在 mongo 3.6.10 中),但它返回空分数数组以及具有历史记录的数组

db.getCollection('student_scores').find({
  "data.marks.score.history": {
    $not: {
      $type: 10
    },
    $exists: true
  }
},
{
  "extensions.rollNo": 1,
  "data.marks.score.history": 1
})

期望的输出是

{
  "extensions": {
    "rollNo": "1"
  },
  "data": [
    {
      "marks": [
        {
          "Score": [
            {
              "history": "90"
            }
          ]
        }
      ]
    }
  ]
}

最佳答案

我使用了类似以下的内容;

db.getCollection('student_scores').aggregate([
  {
    $unwind: "$data"
  },
  {
    $unwind: "$data.marks"
  },
  {
    $unwind: "$data.marks.Score"
  },
  {
    $match: {
      "data.marks.Score.history": {
        $exists: true,
        $not: {
          $type: 10
        }
      }
    }
  },
  {
    $project: {
      "extensions.Roll No": 1,
      "data.marks.Score.history": 1
    }
  },
  {
    $group: {
      _id: "$extensions.Roll No",
      history_grades: {
        $push: "$data.marks.Score.history"
      }
    }
  }
])

根据您的输入,我得到了以下结果(我认为比您预期的输出更具可读性);

[
  {
    "_id": "1",
    "history_grades": [
      "90"
    ]
  }
]

其中 _id 表示任何给定数据集的“extensions.Roll No”值。

你觉得怎么样?

check with a bigger input on mongoplayground

关于arrays - 避免 mongodb 中的空数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57681082/

相关文章:

linux - ulimits 中的 nproc 和 nofile 是什么?

node.js - 使用findOneAndUpdate如何检查文档是否被插入或更新?

arrays - Matlab中数值数组和符号数组的乘法

c++ - 在 vector 中查找字符串的索引

javascript - 在 JavaScript 中将数组排序为 "rows"

mongodb - 查找在指定日期之前创建的所有对象

java - 如何将另一个对象添加到对象数组中

windows - 无法连接到远程 Azure Windows 计算机上运行的 MongoDB 实例

java - 带有@Field注释的Spring数据mongo聚合

用于匹配 MongoDB 字符串的正则表达式