rptony
rptony

Reputation: 1024

How do you find a mongodb record that is two level deep

I have a mongodb schema which looks like this. How do you search such a document to find the record that matches Name == Brad

{
"_id" : ObjectId("57f580efe2ab6485147d0f71"),
"0" : {
    "Name" : "Brad",
    "Age" : 42
},
"1" : {
    "Name" : "Paul",
    "Age" : 32
}

}

The schema is handed over to me by the library I use from a swift program to save a JSON string. The library is Perfect MongoDB.

Upvotes: 2

Views: 581

Answers (1)

TomG
TomG

Reputation: 2529

This can't be done with pure mongo, I suggest you change the schema. But, it is possible using $where:

db.test.find({
    $where: function() {
        for (var prop in this) {
            if (this[prop]["Name"] == "Brad") {
                return true;
            }
        }
        return false;
    }
})

Upvotes: 1

Related Questions