Atlas Search mongo db how to handle null values

64 Views Asked by At

I have a query which I am trying to use in Mongodb Atlas search where I have list of ObjectIds which is centres it can be null or ObjectIds centres = [ObjectId(...), None] so I have to display everything based on the centres list and other filters but when objectIds are there its working fine but when its null it throws error ane even if the centres are null if other filters work I have to show the result

sample of my code

query = {'is_active':True, 'centres':[ObjectId(...), null],...}
query_list = []
for k,v in query.items():
    if k=='centres':
        query_list.append({'in': {'value': v, 'path': k}})
aggregate = {'$search': {'index': 'default', 'compound': {'must': query_list}}}

how can I achieve this since atlas search cannot have null values

I tried using match only for centres and other filters i am using atlas search but that makes query slower. any suggestions

0

There are 0 best solutions below