ThePrestige
ThePrestige

Reputation: 145

Mongodb query to match a value in a subdocument identified by a number

I've got a collection with some elements like this


    ..
     "rMResults" : {
            "4" : {
                "decimal" : 19079,
                "formatted" : "190.79"
            },
            "5" : {
                "decimal" : 200000,
                "formatted" : "2000.00"
            },
            "6" : {
                "decimal" : 9467,
                "formatted" : "94.67"
            },
            "7" : {
                "decimal" : 32360,
                "formatted" : "323.60"
            }
        },
    ..

I would like to query all documents with matching "decimal"

I try with a query like this


    db.getCollection('events').find({  "rMResults": {
        $elemMatch: {
            7: {
                $elemMatch: {
                    decimal: 32360
                }
            }
        }
    } })

but no documents returned, could someone help me?

Upvotes: 0

Views: 92

Answers (1)

Sohan
Sohan

Reputation: 6819

You are trying to fetch the elementMatch on object where it works on arrays. Have a look at elementMatch In simple way to need to match and project the selected fields something like this,

db.getCollection('ACL').find({  "rMResults.7.decimal": 32360}, {"rMResults.7.decimal": true})

Upvotes: 2

Related Questions