hi! some one have create a dynamic query?
i try to build a query with $or but it seems not work, i have try to build a dynamic quesry via js but i fail. some suggest?
I need to skil "Codice" field if is empty
{
$or: [
{
Codice: {{!search_Cod.value ? 0 : parseInt(search_Cod.value)}},
},
{
Descrizione: { "$regex": {{ !search_description.value ? "" : search_description.value }}, "$options": "i"},
Cat_merc: {"$regex": {{ !search_cat_merc.value ? "" : search_cat_merc.value }}, "$options": "i"},
}
]
}
i have try this but return invalid json
{
Descrizione: { "$regex": {{ search_description.value && !isNaN(search_description.value) ? "" : search_description.value }}, "$options": "i"},
Cat_merc: {"$regex": {{ !search_cat_merc.value ? "" : search_cat_merc.value }}, "$options": "i"},
Codice: { {{ search_description.value && isNaN(search_description.value) ? undefined : parseInt(search_description.value) }} }
}
This is what I've done in the past.
[{$match: {
title: {{ searchTitle.value !== "" ? { $regex: searchTitle.value, $options: 'i' } : {$exists:true} }},
}}]
However, I've found it's much faster to just query for everything from Monogo, and then use filter locally.
i have solved with this
{
Descrizione: { "$regex": {{ search_description.value && isNaN(search_description.value) ? search_description.value : "" }}, "$options": "i"},
Cat_merc: {"$regex": {{ !search_cat_merc.value ? "" : search_cat_merc.value }}, "$options": "i"},
Codice: {{ search_description.value && !isNaN(parseInt(search_description.value)) ? parseInt(search_description.value) : undefined }},
}
1 Like