3
"features" : {
    "en" : [ 
        {
            "translatable" : true,
            "capacity " : [ 
                "128GB", 
                "256GB"
            ]
        }, 
        {
            "translatable" : true,
            "material  " : [ 
                "Glass", 
                "Aluminium"
            ]
        }
    ]
}

I am finding 'capacity': '128GB' when I am using this query

db.getCollection('products').find({
    'features.en' : {
        $elemMatch : {
            'capacity' : {
                $in : ['128GB']
            }
        }
    }
})

But not fetching. If i query for 'translatable':true

db.getCollection('products').find({
    'features.en' : {
        $elemMatch : {
            'translatable' : true
        }
    }
})
0

2 Answers 2

2

there is a tricky TYPO in your input document - so please check if capacity has a SPACE at the end

db.prod.aggregate([{
            $match : {
                "features.en.capacity " : "128GB"
            }
        },
    ]).pretty()

to get only array element that mets your criteria you can use this aggregation query:

db.prod.aggregate([{
            $unwind : "$features.en"
        }, {
            $match : {
                "features.en.translatable" : true
            }
        }, {
            $match : {
                "features.en.capacity " : "128GB"
            }
        },
    ]).pretty()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, you are right, it wasn't working because of the TYPO
0

Have you tried this

db.products.find({'features.en': { $elemMatch: {'capacity': {$elemMatch:{$in: ['128GB']}}} } })

Comments

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.