0

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": ""
      }
    }
  }
rickhg12hs
  • 10,638
  • 6
  • 24
  • 42
Bonifacius Sarumpaet
  • 1,263
  • 1
  • 8
  • 21
  • Would you show your desired output? – rickhg12hs Apr 02 '22 at 23:41
  • @rickhg12hs 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": "" }}} – Bonifacius Sarumpaet Apr 04 '22 at 01:21

1 Answers1

1

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.

rickhg12hs
  • 10,638
  • 6
  • 24
  • 42
  • Thanks for your answer, it works as desired! Btw, can you send me the link on where to learn all of this mongodb query? Tbh, I find myself struggling in understanding all of this command. – Bonifacius Sarumpaet Apr 04 '22 at 07:22
  • Glad it helped! You could start with the [MongoDB reference manual](https://www.mongodb.com/docs/manual/crud/) or the tutorials there. You'll also learn a lot from answering questions here and trying things on [mongoplayground.net](https://mongoplayground.net/p/MCj0Pnz4Lfo) and in Compass like you have done. I know I have. – rickhg12hs Apr 04 '22 at 08:09