1

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.

sush_28
  • 11
  • 3
  • Possible duplicate of [How to get a specific embedded document inside a MongoDB collection?](https://stackoverflow.com/questions/10043965/how-to-get-a-specific-embedded-document-inside-a-mongodb-collection) – dnickless Oct 03 '18 at 23:22
  • https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection – dnickless Oct 03 '18 at 23:29

1 Answers1

0

You're almost there. You're just missing the positional $ operator in the projection:

db.RiskScore.find({"country":"Setif","category.categoryName":"Crime"},{"category.$.categoryRating":1})
dnickless
  • 10,733
  • 1
  • 19
  • 34
  • Thank you.. This returns { "_id" : ObjectId("5bab53afcfbc7829bc6356a0"), "category" : [ { "categoryRating" : 3, "categoryName" : "Crime", "categoryNameNew" : "Crime" } ] } and solves the problem. – sush_28 Oct 05 '18 at 09:09
  • Also, Is there a way only the categoryRating field can be obtained? – sush_28 Oct 05 '18 at 09:10
  • Yes, through the aggregation framework. I would suggest to do it on the client side if your document is so small. Otherwise, please update your question with the exact definition of what you would want the output to look like and I'll get you sorted. – dnickless Oct 05 '18 at 09:50