Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node api to fetch subdocuments using mongoose

My MongoDB stores documents having the following structure:

{
    "application_detail":{},
    "curl_detail":{
        "Curl1":{
            "key1":"value1",
            "key2":"value2"
        },
        "Curl2":{
            "key1":"value1",
            "key2":"value2"        
        },
        "Curl3":{
            "key1":"value1",
            "key2":"value2"
        },
        "Curl4":{
            "key1":"value1",
            "key2":"value2"
        },
        /*total number of curls are unknown*/
    }
}

How can I fetch key1 for all the curls present under curl_detail from mongoDB using express and mongoose?

Expected Output:

{
    "curl_detail": {
        "Curl1": {
            "key1": "value1"
        },
        "Curl2": {
            "key1": "value1"
        },
        "Curl3": {
            "key1": "value1"
        },
        "Curl4": {
            "key1": "value1"
        }
    }
}

NOTE: Total number of curls are unknown.

like image 957
Kapil Khandelwal Avatar asked Mar 29 '26 01:03

Kapil Khandelwal


1 Answers

I would like to propose you changing data structrure from

"curl_detail": {
    "Curl1": {
        "key1": "value1",
        "key2": "value2"
    },
    "Curl2": {
        "key1": "value1",
        "key2": "value2"
    }
}

to

"curl_detail": [{
        "key1": "value1",
        "key2": "value2"
    },
    {
        "key1": "value1",
        "key2": "value2"
    }
]

Storing data structure in array will allow you to find some data in an easier way. Also there's no need to have object with properties like Curl1, Curl2 etc.

For the new data structure there are already some answers with the explanation:

1) Mongo find value with unknown parent key

2) Query MongoDB by value when parent key is unknown

3) MongoDB: Find document given field values in an object with an unknown key

UPDATE

If there's no possibility to change the data structure please see this solution (trick with use of $arrayToObject / $objectToArray to determine the name of nested key e.g. "Curl1"):

db.collection.aggregate([{
        $addFields: {
            curl_detail: {
                $arrayToObject: {
                    $map: {
                        input: {
                            $objectToArray: "$curl_detail"
                        },
                        as: "details",
                        in: {
                            k: "$$details.k",
                            v: {
                                key1: "$$details.v.key1"
                            }
                        }
                    }
                }
            }
        }
    },
    {
        $project: {
            _id: 0,
            curl_detail: 1
        }
    }
])

This outputs the following:

[
  {
    "curl_detail": {
      "Curl1": {
        "key1": "value1"
      },
      "Curl2": {
        "key1": "value1"
      },
      "Curl3": {
        "key1": "value1"
      },
      "Curl4": {
        "key1": "value1"
      }
    }
  }
]

You can check it through Mongo Playground

like image 106
Hlib Derbenov Avatar answered Mar 31 '26 05:03

Hlib Derbenov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!