如何使用Compass MongoDB中的关键通配符从嵌套对象中找到值?

发布于 2025-01-18 12:51:09 字数 1531 浏览 0 评论 0原文

我在 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": ""
    }
}

我如何返回类型为“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": ""
      }
    }
  }

I have one nested object in 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": ""
    }
}

How did I return the document WHERE type is "select-one"? (I want to find which key in data_rm has the type of select-one)

** EDIT **

The desired output is:

  {
    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": ""
      }
    }
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

(り薆情海 2025-01-25 12:51:09

这是您可以做到的一种方法。在 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 上尝试一下。

Here's one way you could do it. In Compass you can enter each stage of the aggregation pipeline.

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" } }
  }
])

Try it on mongoplayground.net.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文