BigHairDev
BigHairDev

Reputation: 59

MongoDB query for selecting values from nested array collection

I am newbie to Mongo and trying to figure out how to parse through a complex nested collection.

{
    "_id": ObjectId("idVal"),
    "Ids": [{
    "mar": [{
        "se": [{
            "gootics": "UA-3-1"
        }],
        "usery": [{
            "fc0633ac-81": [{
                "sep": [{
                    "custS": "",
                    "promer": "0",
                    "reaivity": "20",
                    "stus": "active",
                    "urlist": "",
                    "upe": [
                        "mat "
                    ],
                    "whlete": ""
                }],
                "uspt": [{
                    "cor": "000000",
                    "foron": "hthp",
                    "sla": "Join!",
                    "slie": "httpsg",
                    "slile": "Jost:",
                    "suody": "We'll be in touch.",
                    "sumer": "Awesome!",
                    "tte": "qumail"
                }, {
                    "cr": "000000",
                    "refeion": "htve",
                    "sla": "Go!",
                    "slige": "httg",
                    "slle": "Hes",
                    "slle": "Mm:",
                    "tte": "rk"
                }]
            }]
        }]
    }],
    And many more such inner array’ s,
    comma separated
    }]
}

I need to select the whole part of mar with a mongo query or php. db.collectionname.find({"Ids" : "mar"}).pretty().

This isn’t resulting anything, just being blank and tried doing db.collectionname.find({}).pretty(), it worked well, got all the results.

I did tried with $elemMatch , $pop etc.

Many solutions are taking very small and easy json files, no one have traversed or parsed through such complex json string. Refer some other standard process or provide your answer.

My need in zist: I will just have one of the Id's value, need to query on the basis of Id. like, select * from db where Id's == Id I have; in this case it can be mar

Can anyone help me with writing the same query in php?? and printing the output? I am not able to do that. "$ActualData = $collection->find(array("Ids.mar : 1"));" but this isn't printing any value.

var_dump($array);
// Above line is printing this:
array (size=0)
  empty

var_dump($ActualData);
//Above line is printing this:
object(MongoCursor)[4]

//not being executed at all:
foreach($ActualData as $doc)
{
    echo "Inside it is";
    var_dump($doc);

} 
echo "Count:".count($ActualData)."\n";
//It does have 1 document
Count:1

Upvotes: 1

Views: 2578

Answers (1)

Vishwas
Vishwas

Reputation: 7067

It is strongly recommended that you should not use dynamic values as keys.

You should use projection in query, if you want to get mar.

db.collection.find({},{"Ids.mar":1})

In this case, You must know key mar.

In PHP, you can convert above query as following:

$data = $collection->find(array(),array("Ids.mar" => 1));

OR

// Search criteria
$query = array();
// Projection (fields to include)
$projection =  array("Ids.mar" => 1);
$cursor = $collection->find($query, $projection);
foreach ($cursor as $doc) {
    //print or process here
}

Upvotes: 1

Related Questions