0

I've asked this question before but not in the clearest way since I had no responses :( so I thought I would try again.

I have a document as shown below, I want to create a new document which only picks the names in the array where language = "English".

{
"_id" : ObjectId("564d35d5150699558156942b"),
"objectCategory" : "Food",
"objectType" : "Fruit",
"objectName" : [ 
    {
        "language" : "English",
        "name" : "Apple"
    }, 
    {
        "language" : "French",
        "name" : "Pomme"
    }, 
    {
        "language" : "English",
        "name" : "Strawberry"
    }, 
    {
        "language" : "French",
        "name" : "Fraise"
    }
]

}

I want the $out document to look like this below. I know I can filter a document by content but, I want to filter within a single document not across a collection. For getting the right document in the first place, I would have a query to $find objectCategory = "Food" and objectType = "Fruit"

{
"_id" : ObjectId("564d35d5150699558156942b"),
"objectCategory" : "Food",
"objectType" : "Fruit",
"objectName" : [ 
      "name" : "Apple",
      "name" : "Strawberry"
]

} Thanks, Matt

Matt Lightbourn
  • 597
  • 3
  • 20

1 Answers1

0

wow, ah, I really thought I found it with: db.serviceCatalogue.find({objectName: {"$elemMatch": {language: "English"}}}, {"objectName.name": 1})

;thanks to: Retrieve only the queried element in an object array in MongoDB collection

However, it did nothing, I must have dreamt it worked. How do you just get the array positions where the value of a field called language = 'English'?

this is only an example of what I want to do, it seems like this is just painful, especially with no-one answering other than me :)

Community
  • 1
  • 1
Matt Lightbourn
  • 597
  • 3
  • 20
  • ah, shame. It works on one document but not on another. Can't understand why. Check out http://stackoverflow.com/questions/33728093/aggregate-using-push-match-and-out where I tried db.globalData.find({businessType: {"$elemMatch": {language: "English"}}}, {"businessType.businessTypeDesc": 1}) and it's ignored my request and just shown me all the documents with less detail but has both French and English - really annoying! – Matt Lightbourn Nov 19 '15 at 03:31