如何在 Compass MongoDB 中使用键通配符从嵌套对象中查找值?
How to find value from nested object with key wildcard in Compass MongoDB?
我在 Compass 中有一个嵌套对象 MongoDB:
{
"data_rm": {
"pembiayaan": {
"name": "pembiayaan",
"value": "asuransi",
"type": "radio",
"title": ""
},
"asuransi": {
"name": "asuransi",
"value": "ADMEDIKA - FWD LIFE INDONESIA (FINANSIAL WIRAMITRA DANADYAKSA, PT)",
"type": "select-one",
"text": "ADMEDIKA - FWD LIFE INDONESIA (FINANSIAL WIRAMITRA DANADYAKSA, PT)",
"title": ""
},
"informasi_diperoleh_dari": {
"name": "informasi_diperoleh_dari",
"value": "pasien",
"type": "radio",
"title": ""
},
"cara_datang": {
"name": "cara_datang",
"value": "sendiri",
"type": "radio",
"title": ""
},
"nama_pengantar": {
"name": "nama_pengantar",
"value": "Tn. BAGUS",
"type": "text",
"title": ""
},
"no_telp_pengantar": {
"name": "no_telp_pengantar",
"value": "0813xxxxxxxx",
"type": "text",
"title": ""
}
}
我如何 return 文件类型为“select-one”? (我想找到 data_rm 中的哪个键具有 select-one 的类型)
** 编辑 **
期望的输出是:
{
data_rm: {
"asuransi": {
"name": "asuransi",
"value": "ADMEDIKA - FWD LIFE INDONESIA (FINANSIAL WIRAMITRA DANADYAKSA, PT)",
"type": "select-one",
"text": "ADMEDIKA - FWD LIFE INDONESIA (FINANSIAL WIRAMITRA DANADYAKSA, PT)",
"title": ""
}
}
}
这是您可以做到的一种方法。在 Compass 中,您可以进入聚合管道的每个阶段。
db.collection.aggregate([
{
"$set": { "rmArray": { "$objectToArray": "$data_rm" } }
},
{
"$set": {
"selOne": {
"$filter": {
"input": "$rmArray",
"cond": { "$eq": [ "$$this.v.type", "select-one" ] }
}
}
}
},
{
"$replaceWith": { "data_rm": {"$arrayToObject": "$selOne" } }
}
])
在 mongoplayground.net 上试用。
我在 Compass 中有一个嵌套对象 MongoDB:
{
"data_rm": {
"pembiayaan": {
"name": "pembiayaan",
"value": "asuransi",
"type": "radio",
"title": ""
},
"asuransi": {
"name": "asuransi",
"value": "ADMEDIKA - FWD LIFE INDONESIA (FINANSIAL WIRAMITRA DANADYAKSA, PT)",
"type": "select-one",
"text": "ADMEDIKA - FWD LIFE INDONESIA (FINANSIAL WIRAMITRA DANADYAKSA, PT)",
"title": ""
},
"informasi_diperoleh_dari": {
"name": "informasi_diperoleh_dari",
"value": "pasien",
"type": "radio",
"title": ""
},
"cara_datang": {
"name": "cara_datang",
"value": "sendiri",
"type": "radio",
"title": ""
},
"nama_pengantar": {
"name": "nama_pengantar",
"value": "Tn. BAGUS",
"type": "text",
"title": ""
},
"no_telp_pengantar": {
"name": "no_telp_pengantar",
"value": "0813xxxxxxxx",
"type": "text",
"title": ""
}
}
我如何 return 文件类型为“select-one”? (我想找到 data_rm 中的哪个键具有 select-one 的类型)
** 编辑 **
期望的输出是:
{
data_rm: {
"asuransi": {
"name": "asuransi",
"value": "ADMEDIKA - FWD LIFE INDONESIA (FINANSIAL WIRAMITRA DANADYAKSA, PT)",
"type": "select-one",
"text": "ADMEDIKA - FWD LIFE INDONESIA (FINANSIAL WIRAMITRA DANADYAKSA, PT)",
"title": ""
}
}
}
这是您可以做到的一种方法。在 Compass 中,您可以进入聚合管道的每个阶段。
db.collection.aggregate([
{
"$set": { "rmArray": { "$objectToArray": "$data_rm" } }
},
{
"$set": {
"selOne": {
"$filter": {
"input": "$rmArray",
"cond": { "$eq": [ "$$this.v.type", "select-one" ] }
}
}
}
},
{
"$replaceWith": { "data_rm": {"$arrayToObject": "$selOne" } }
}
])
在 mongoplayground.net 上试用。