I am new to MongoDB and need help with projecting an array element when another array element is found.
Below is an example of my document-
{
"_id": ObjectId("5bab53afcfbc7829bc6356a0"),
"category": [
{
"categoryRating": 3,
"categoryName": "Crime",
"categoryNameNew": "Crime"
},
{
"categoryRating": 4,
"categoryName": "Security Services",
"categoryNameNew": "Other"
},
{
"categoryRating": 4,
"categoryName": "Civil Unrest",
"categoryNameNew": "Societal"
},
{
"categoryRating": 4,
"categoryName": "Terrorism",
"categoryNameNew": "Terrorism"
},
{
"categoryRating": 3,
"categoryName": "Kidnapping",
"categoryNameNew": "Terrorism"
},
{
"categoryRating": 4,
"categoryName": "Geopolitical",
"categoryNameNew": "Geopolitical"
}
],
"country": "Setif",
"apiSource": "iJET",
"overallScore": 4,
"newDate": "2017-12-28T19:52:58Z"
}
I want the categoryRating
value when I specify that categoryName
is "Crime". So, in this case, I need it to print categoryrating
as 3.
So far, I have tried the following query-
db.RiskScore.find({"country":"Setif","category.categoryName":"Crime"},{"category.categoryRating":1})
The following is the result of my query-
{
"_id": ObjectId("5bab53afcfbc7829bc6356a0"),
"category": [
{ "categoryRating": 3 },
{ "categoryRating": 4 },
{ "categoryRating": 4 },
{ "categoryRating": 4 },
{ "categoryRating": 3 },
{ "categoryRating": 4 }
]
}
It returns all the categoryRatings instead of just that where the categoryName is Crime. Please, any help with respect to this would be highly appreciated.