Home > OS >  mongo - accessing a key-value field while filtering
mongo - accessing a key-value field while filtering

Time:06-06

I have these data:

myMap = {
  "2": "facing",
  "3": "not-facing"
  "1": "hidden"
}  
   
 

stages [
 {
   "k": 1,
   "v": "hidden"
 },
 {
   "k": 2,
   "v": "facing"
 },
 {
   "k": 3,
   "v": "not-facing"
 }
]  
 

and a aggregate query but, I'm missing a syntax to dynamically fetch the map data:

db.MyCollection.aggregate()
   .addFields({
      myMaps:  myMap 
   })
   .addFields({
     stages: stages
    })
   .addFields({
     process: {
         $filter: {
            input: stages,
            as: stageData,
            cond: {$eq: [$$stageData.v, $myMaps[$$stageData.k]]} 
         }
      }
    })  

As you may already note, this syntax: $myMaps[$$stageData.k] doesn't work, how should I access the myMaps based on the value of the k in stageData ?

CodePudding user response:

Query

  • like your query set mymap and stages as extra field
  • mymapKeys is an extra field added (you can $unset is in the end)
  • filter the stages, and if stage.k is contained in mymapKeys we keep that member

*not sure if this is what you need, but looks like from your query in mongodb query language we dont have getField(doc,$$k) we only have getField(doc,constant_string) which cant be used in your case, so it costs here more than a hashmap lookup, here its like linear cost (check if member in the array). For arrays we have $getElementAt(array,$$k) if those numbers are always in sequence, 1,2,3 etc you might be able to use arrays instead of objects

Playmongo

aggregate(
[{"$set": {"mymap": {"2": "facing", "3": "not-facing", "1": "hidden"}}},
 {"$set": 
   {"mymapKeys": 
     {"$map": {"input": {"$objectToArray": "$mymap"}, "in": "$$this.k"}}}},
 {"$set": 
   {"stages": 
     [{"k": 1, "v": "hidden"}, {"k": 2, "v": "facing"},
       {"k": 3, "v": "not-facing"}]}},
 {"$set": 
   {"process": 
     {"$filter": 
       {"input": "$stages",
        "cond": {"$in": [{"$toString": "$$this.k"}, "$mymapKeys"]}}}}}])
  • Related