0

I had one document with multiple array elements. I want to find some specific values from array.

{
    "_id" : ObjectId("54a67aa569f2bc6a5865f220"),       
    "language" : "us_english",
    "country" : "united states",
    "state" : "texas",
    "words" : [ 
        {

            "handle" : "gm",
            "replacement" : "good morning",

        }, 
        {

            "handle" : "ilu",
            "replacement" : "i love you",

        }
    ]
}

I want find value of replacement where handle is gm and language is english , country is united states and state is texas.

I tried many solutions but i cant able to find specific value from document.

Is there any solution ??

rajesh
  • 285
  • 4
  • 19

1 Answers1

1

You can use aggregation operation.

  • $match to filter the documents
  • $unwind to deconstruct the words field
  • Reuse $match to find value of replacement field

    db.collection.aggregate([{$match : {language : 'us_english', country: 'united states', state: 'texas'}}, {$unwind: '$words'}, {$match: {"words.handle" : "gm"}}])
    
    { "_id" : ObjectId("54a67aa569f2bc6a5865f220"), "language" : "us_english", "country" : "united states", "state" : "texas", "words" : { "handle" : "gm", "replacement" : "good morning" } }
    
styvane
  • 59,869
  • 19
  • 150
  • 156