I have following collection
{
"_id" : ObjectId("5b1fd21cc1448a422a21b3cc"),
"amount" : 500,
"bank" : "ICICI",
"status" : "draft",
"currency" : "EURO",
"senderId" : ObjectId("5a16ca832bedaa6c4cb4ad97"),
"created_date" : ISODate("2018-06-12T00:00:00.000Z")
}
I am searching for multiple criteria in it such like
const array = [
{ "status": { "$regex": searchString }},
{ "amount": { "$regex": parseInt(searchString) }},
{ "created_date": searchString },
{ "currency": { "$regex": searchString }},
]
Invoice.aggregate([
{ "$match": { "$and":[{ "$or": array }] }}
])
But In above case $regex on amount searching in not working... It throws me an error...
How can I do this? I need to do this in aggregate query because I have to run $lookup for senderId
$regexon anything which is not a string.$matchno. You look like you're approaching this wrong anyway as you simply do not want to search "every" field in the document. if you're entering "text" as a search then just search the fields which have "text". If you want to let someone search by "amount", then make that a separate field in your search function and/or form. It's just makes no sense to look for "Hello" in a field which has only numeric values. And vice versa. Take the design tip and do it better.$toStringor any other aggregation operator?