raffis / mongodb-query-exporter

Prometheus MongoDB aggregation query exporter
MIT License
93 stars 26 forks source link

Query problem with a "like" match value #268

Closed cerealekiller-mike closed 9 months ago

cerealekiller-mike commented 9 months ago

Describe the bug

I'm trying to build an exporter to retrieve all documents where the Id match for 2 conditions

To Reproduce

My pipeline is

[ { "$sort":{ "createDate":-1 } }, { "$match":{ "_id": {"$in":["/123./", "/134./"]} } }, { "$project": { "_id": 0, "createDate": { "$dateToString": { "format": "%d/%m/%Y %H:%M:%S", "date": "$createDate" } } } }, { "$limit": 1 } ]

Expected behavior

In my case when I put a direct _id like '1234567' the aggeration works but if I put a like /123./ the query returns nothing. It's looks like the "match" condition is not correctly written.

Environment

Additional context

There a specific syntax for this case ?

Thanks

cerealekiller-mike commented 9 months ago

I've opened as a bug but in fact is a question. Sorry

raffis commented 9 months ago

Are you attempting to do a regex match? This won't work like this. The regex in quotes here is just a simple string. You need to use $regex (but that won't work with $in) => https://www.mongodb.com/docs/manual/reference/operator/query/regex/#-regex-vs.--pattern--syntax