我有以下文件:
[
{
"_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
.如何只更改缺少的字段,而不更改整个对象?
敬请指教.谢谢.