jeroen / mongolite

Fast and Simple MongoDB Client for R
https://jeroen.github.io/mongolite/
286 stars 65 forks source link

Using regex in match stage of aggregation not working #155

Closed ramanan82 closed 5 years ago

ramanan82 commented 5 years ago

I have tried the equivalent in other client libraries (pymongo in python), and it works fine. However, when I try the following in mongolite, it returns a blank document; in fact, the query does not even go through to the server, as I have closely seen the logs:

In R, I tried

user_counts_ppl <- list(
  list(
    `$match` = list(
      `_source.text` = list(
        `$regex` = '\\w+'
      )
    ),
    `$group` = list(
      `_id` = list(
        sourcename = '$_source.sourcename',
        username = '$_source.username'
      ),
      `Messages` = list(`$sum` = 1)
    )
  ),
  list(
    `$addFields` = list(
      `Source Type` = '$_id.sourcename',
      `Username` = '$_id.username'
    )
  )
) %>% toJSON(auto_unbox = TRUE)

which results in:

[
    {
        "$match": {
            "_source.text": {
                "$regex": "\\w+"
            }
        },
        "$group": {
            "_id": {
                "sourcename": "$_source.sourcename",
                "username": "$_source.username"
            },
            "Messages": {
                "$sum": 1
            }
        }
    },
    {
        "$addFields": {
            "Source Type": "$_id.sourcename",
            "Username": "$_id.username"
        }
    }
]

However, the regex "\\w+" works fine in a regular collection$find() query.

jeroen commented 5 years ago

Is the actual regex supposed to be \w or \\w? If it is the latter, try escaping both backslashes, i.e: \\\\w?

ramanan82 commented 5 years ago

Thanks for the response, @jeroen

I actually enjoy mongolite a lot.

The actual regex is \w only, which corresponds to \\w in my R code. The same works in collection$find() query, as I stated in my earlier comment.

And just for the sake of it, I tried escaping both backslashes, too... As in \\\\w in the R code.

ramanan82 commented 5 years ago

I have managed to get it working after updating to the latest version of mongolite (2.0). It now shows the error generated by the backend. Basically, I had to give an $options field inside the regex. It works now!