I'm trying to optimize a query of form A.X = value OR B.Y.Z = value OR C.H = value in Mongodb. DB engine uses 3 different indices to perform a query like this and then join results. It's somewhat slow so I decided to put everything in one array and create a multikey index on it. So I created an index and this query to update entries and populate it:
{
'$addFields': {
'AllowedEntries': [ '$A.X', '$B.Y.Z', '$C.H' ]
}
}
It worked like a charm but then I realized that some of values are nulls and I want nulls out of array. So I went to this query:
{
'$addFields': {
'AllowedEntries': {
'$map': {
'input': [ '$A.X', '$B.Y.Z', '$C.H' ],
'as': 'item',
'in': {
'columns': {
'$filter': {
'input': '$$item',
'as': 'elt',
'cond': { '$ne': [ '$$elt', null ] }
}
}
}
}
}
}
}
Which doesn't work because of input to $filter must be an array not binData error. How can I do it atomically? In theory I could execute this query and then pull nulls out but I'd really like to do it in one update.
$addToSetbut failed. Also don't you want to post an answer? I'l mark it since it helped me.