我有以下文件:

[
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892a8)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": [],
        "tag": "ACCOUNT_NO"
      },
      "doc_type_class_labels": ""
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892ab)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": [
          {
            "start": 1019,
            "end": 1033,
            "text": "F10-749-113245",
            "tag": "ACCOUNT_NO",
            "color": "rgb(244, 10, 10, 0.527)"
          }
        ],
        "tag": "ACCOUNT_NO"
      },
      "doc_type_class_labels": "BL"
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892ae)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": []
      },
      "doc_type_class_labels": ""
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892b1)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": [
          {
            "start": 65,
            "end": 81,
            "text": "Nowhere BANK LTD ",
            "tag": "DRAWER",
            "color": "rgb(85, 239, 239, 0.527)"
          }
        ],
        "tag": "ACCOUNT_NO"
      },
      "doc_type_class_labels": "APPLICATION"
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892b4)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": [],
        "tag": "ACCOUNT_NO"
      },
      "doc_type_class_labels": "APPLICATION"
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892b7)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": []
      },
      "doc_type_class_labels": ""
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cbfdaa015ee5fc8892bf)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": []
      },
      "doc_type_class_labels": ""
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cbfdaa015ee5fc8892c2)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": []
      },
      "doc_type_class_labels": ""
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cbfdaa015ee5fc8892c5)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": []
      },
      "doc_type_class_labels": ""
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cbfdaa015ee5fc8892c8)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": []
      },
      "doc_type_class_labels": ""
    },
    "category": "TEXT"
  }
]

我运行了以下查询来获取所需的数据:

{
    '$unwind': {
        'path': '$annotations.ner_labels.value',
        "preserveNullAndEmptyArrays": True
    }
},

我得到以下输出:

[
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892a8)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "tag": "ACCOUNT_NO"
      },
      "doc_type_class_labels": ""
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892ab)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": {
          "start": 1019,
          "end": 1033,
          "text": "F10-749-113245",
          "tag": "ACCOUNT_NO",
          "color": "rgb(244, 10, 10, 0.527)"
        },
        "tag": "ACCOUNT_NO"
      },
      "doc_type_class_labels": "BL"
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892ae)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {},
      "doc_type_class_labels": ""
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892b1)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": {
          "start": 65,
          "end": 81,
          "text": "nowhere BANK LTD ",
          "tag": "DRAWER",
          "color": "rgb(85, 239, 239, 0.527)"
        },
        "tag": "ACCOUNT_NO"
      },
      "doc_type_class_labels": "APPLICATION"
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892b4)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "tag": "ACCOUNT_NO"
      },
      "doc_type_class_labels": "APPLICATION"
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892b7)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {},
      "doc_type_class_labels": ""
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cbfdaa015ee5fc8892bf)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {},
      "doc_type_class_labels": ""
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cbfdaa015ee5fc8892c2)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {},
      "doc_type_class_labels": ""
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cbfdaa015ee5fc8892c5)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {},
      "doc_type_class_labels": ""
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cbfdaa015ee5fc8892c8)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {},
      "doc_type_class_labels": ""
    },
    "category": "TEXT"
  }
]

preserveNullAndEmptyArrays分创造了empty object分.现在我需要在empty object中添加一个临时值.因此,我使用了以下代码:

{
    '$project': {
        'annotations.ner_labels.value': {
            '$ifNull': [
                '$annotations.ner_labels.value', [],
            ]
        },
        'category': 1,
        'job_id': 1
    }
},

在此之后,如果是特定的字段,则修改整个对象,以下是输出:

[
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892a8)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": []
      }
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892ab)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": {
          "start": 1019,
          "end": 1033,
          "text": "F10-749-113245",
          "tag": "ACCOUNT_NO",
          "color": "rgb(244, 10, 10, 0.527)"
        }
      }
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892ae)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": []
      }
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892b1)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": {
          "start": 65,
          "end": 81,
          "text": "nowhere BANK LTD ",
          "tag": "DRAWER",
          "color": "rgb(85, 239, 239, 0.527)"
        }
      }
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892b4)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": []
      }
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cae9aa015ee5fc8892b7)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": []
      }
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cbfdaa015ee5fc8892bf)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": []
      }
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cbfdaa015ee5fc8892c2)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": []
      }
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cbfdaa015ee5fc8892c5)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": []
      }
    },
    "category": "TEXT"
  },
  {
    "_id": "ObjectId(6448cbfdaa015ee5fc8892c8)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": []
      }
    },
    "category": "TEXT"
  }
]

以下是

{
    "_id": "ObjectId(6448cbfdaa015ee5fc8892c2)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {},
      "doc_type_class_labels": ""
    },
    "category": "TEXT"
}

ifnull之后更改为以下值

{
    "_id": "ObjectId(6448cbfdaa015ee5fc8892c2)",
    "job_id": "ObjectId(6448cac2aa015ee5fc8892a0)",
    "annotations": {
      "ner_labels": {
        "value": []
      }
    },
    "category": "TEXT"
}

缺少字段doc_type_class_labels.如何只更改缺少的字段,而不更改整个对象? 敬请指教.谢谢.

推荐答案

you can use $addFields instead of $project since project will not show the fields that you do not include.
The $addFields stage is equivalent to a $project stage that explicitly specifies all existing fields in the input documents and adds the new fields.

playground

db.collection.aggregate([
  {
    $unwind: {
      path: "$annotations.ner_labels.value",
      preserveNullAndEmptyArrays: true
    }
  },
  {
    $addFields: {
      "annotations.ner_labels.value": {
        $ifNull: [
          "$annotations.ner_labels.value",
          []
        ]
      }
    }
  }
])

Mongodb相关问答推荐

MongoDB通过查找具有多个数组的对象进行聚合

在不知道字段名称的情况下如何引用 MongoDB 中的字段?

MongoDB 聚合 - $project 和 $match 阶段未按预期工作

MongoDb聚合查询问题

Mongoose 聚合和多级组

在运算符 $map 中嵌入运算符 $exists

Mongoose 更新不同类型的记录

在我的查询中使用 populate() 时的 MongoDB createIndex()

如何使用 Golang 库获取 MongoDB 版本?

使用绝对类型在 Typescript 中编写 Mongoose 的类型化模型和模式的类和接口

解析命令行时出错:unrecognized option --rest

如何在 mongodb 中设置整数的默认值?

Stripe:必须提供来源或客户

在MongoDB中查询一个半​​径内的位置

判断 mongoDB 是否连接

查询不等于 null 或空的地方

如何在 golang 和 mongodb 中通过 id 查找

使用 C# 聚合 $lookup

mongodb安装失败运行mongod

mongodb & max connections最大连接数