I am having a MongoDB document like this
{
"_id" : "toy_in_en_L02B",
"modelCode" : "L02B",
"path" : "/toy/in/en",
"dataPath" : "/toy/in/en/L02B",
"basicModelInformation" : {
"name" : "News",
"startingPrice" : "1234.0"
},
"grades" : {
"grades" : [
{
"key" : "LVL001",
"dataPath" : "/toy/in/en/L02B",
"name" : "XE P",
"price" : "1234.0"
},
{
"key" : "LVL002",
"dataPath" : "/toy/in/en/L02B",
"name" : "XE P",
"price" : "1234.0"
},
{
"key" : "LVL003",
"dataPath" : "/toy/in/en/L02B",
"name" : "XE P",
"price" : "1234.0"
},
{
"key" : "LVL004",
"dataPath" : "/toy/in/en/L02B",
"name" : "XE P",
"price" : "1234.0"
}
]
}
}
Now I want to retrieve a complete Document with having only one grade as LVL001. But when I am querying this like
db.getCollection('models').find({$and: [{"dataPath" : "/nissan/in/en/L02B" }, {"grades.grades" : {$elemMatch: {"key":"LVL002"}}}]})
I am getting all the grades.
I already checked the thread Retrieve Queried Element but not getting it.
My code is using the Spring @Query. Any json query parameter for this will also be useful.