mongodb - Compare Size of Arrays Inside an Array of Objects -


i want find documents scompetitions.length greater competitions.length.

here's sample documents document:

{     "_id" : objectid("59b28f432b4353d3f311dd1b"),     "name" : "ford focus rs 2008",     "requirements" : [          {             "ranktype" : "d1",             "competitions" : [                  objectid("59b151fd2b4353d3f3116827"),                  objectid("59b151fd2b4353d3f3116829")             ],             "scompetitions" : [                  "rallye monte-carlo",                  "rally sweden"             ]         },          {             "ranktype" : "a3",             "competitions" : [                 objectid("59b151fd2b4353d3f3116f6b")             ],             "scompetitions" : [                  "rally italia sardegna",                  "neste rally finland"             ]         }     ] }, {     "_id" : objectid("0000b28f432b4353f311dd1b"),     "name" : "ford focus rs 2012",     "requirements" : [          {             "ranktype" : "d1",             "competitions" : [                  objectid("59b151fd2b4353d3f3116827"),                  objectid("59b151fd2b4353d3f3116829")             ],             "scompetitions" : [                  "rallye monte-carlo",                  "rally sweden"             ]         },          {             "ranktype" : "a3",             "competitions" : [                 objectid("59b151fd2b4353d3f3116f6b"),                 objectid("59b151fd2b4353d3f3116f6b")             ],             "scompetitions" : [                  "rally italia sardegna",                  "neste rally finland"             ]         }     ] } 

so looking @ samples return objectid("59b28f432b4353d3f311dd1b")

my problem requirements array itself, need somehow iterate it

no need "iterate". need $anyelementtrue check after returning results $map. , can inside $redact action:

model.aggregate([   { "$redact": {     "$cond": {       "if": {         "$anyelementtrue": {           "$map": {             "input": "$requirements",             "as": "r",             "in": {                "$gt": [                 { "$size": "$$r.scompetitions" },                 { "$size": "$$r.competitions" }               ]             }           }         }       },       "then": "$$keep",       "else": "$$prune"     }   }} ]) 

so it's simple comparison $size each array element, , if "any" of elements true, document "kept" or otherwise "pruned" results.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -