arrays - Mongo 4.2:删除空字段
问题描述
我的 MongoDB 集合中的文档如下所示:
我的 Mongo 版本是 4.2.3
{
"_id": "SAGE-UW-00005",
"carriers": [
{
"bindable": true,
"carrierCode": "LMICO",
"mapped": true,
"products": [
{
"industries": [
{
"industryCode": null,
"states": "GA"
}
],
"isAllNCCIValid": null,
"isAllstateValid": true,
}
],
"questionCode": "LMGENRL17"
}
],
"column": 1,
"dataType": null,
}
这是我想要的输出:
{
"_id": "SAGE-UW-00005",
"carriers": [
{
"bindable": true,
"carrierCode": "LMICO",
"mapped": true,
"products": [
{
"industries": [
{
"states": "GA"
}
],
"isAllstateValid": true,
}
],
"questionCode": "LMGENRL17"
}
],
"column": 1,
}
我不确定集合中嵌套子文档的深度,但集合中应该有很多空字段。我的后端代码使用 $exists 来查询集合中的字段,因此 null 在这里造成了问题。
解决方案
我不确定集合中嵌套子文档的深度,但是集合中应该有很多空字段
这是一个动态的问题。最好的选择是在删除代码中的空字段后替换文档。
由于您有嵌套级别,我建议您将数据映射到pojo
并检查任何条目和字段是否为空。除非您知道这些字段,否则删除它们效率不高。
推荐阅读
- python - OpenCV 错误:即使文件存在,断言也失败
- python - 如何使用 playsound 模块关闭音乐?
- sql - 在连接语句中使用 LIKE
- c++ - LOG4CXX_INFO 不是#define 的干净包装器
- javascript - 没有全局匹配的正则表达式的 JavaScript 拆分
- javascript - 如何将传递的字符串转换为骆驼案例
- python - 这个 cudaGetDevice() 失败可能是什么问题。状态:未找到 cudaGetErrorString 符号
- javascript - 自省查询对 buildClientSchema 无效
- amazon-web-services - 5 分钟后自动创建已删除的 EC2 (AWS) 实例
- javascript - JavaScript 中的动画坐标 - 预先计算的值与动态计算