1

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
4
  • Yeah! even I was thinking the same, what could be the best practice is to store Id's inside with a "id" column for every group, isn't it?? Commented Jul 17, 2015 at 8:22
  • You can get output using db.collection.find({},{"Ids.mar":1}) only if you know the key mar. It is strongly recommended not to use dynamic values as keys in your schema. Commented Jul 17, 2015 at 10:24
  • It is working very well, Thanks for your comments :) Commented Jul 17, 2015 at 14:23
  • 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. Commented Jul 17, 2015 at 18:17

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry for late response, Thanks for the answer!
Can anyone tell me drawback of using dynamic values as keys? "Data" : { "Campaign Name" : "Campaign 1", "Ad Set Name" : "Adset 1", "Ad Name" : "Ad 1", "URL Tags" : } I have dynamic key too in my scenario, is there anything i need to do about it??
@AbdulMoiz Dynamic keys are difficult to query. If keys are fix, it will be much easier to query on these fields.
@Vishwas: What if we are getting them from an external source, and have no control over it? What would be the best practice in this scenario?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.