This is my DB record:
{"_id":{"$oid":"66926c22d78eca74f8f0d4f4"},"atsId":"734549","clientCode":"keoic","email":"ekintunali@gmail.com","error":"No content to map due to end-of-input\n at [Source: (org.jvnet.mimepull.DataHead$ReadMultiStream); line: 1, column: 0]","importId":"796fe5c5-c189-4577-925e-ec1b76f8c034","importType":"icims-candidate-event","profileType":"person","status":"fail","timestamp":{"$date":{"$numberLong":"1720871970033"}}}
I can run this query successfully:
{$and: [{timestamp: {$gt: {$date: "2024-07-13T11:58:43.614+00:00"}}}, {status: "fail"}]}
But this query fails:
{$and: [
{timestamp: {$gt: {$date: "2024-07-13T11:58:43.614+00:00"}}},
{error: {$regex: "Contains invalid characters"}}
]}
The error says:
We're sorry, an unexpected error has occurred. Check your query and try again.
Each individual clause of the $and operator works separately.
Any idea how to fix this issue?
1 Like
steevej
(Steeve Juneau)
2
The query
seems to work here as it does not generate an error in my case:
mongosh> db.c.findOne( {$and: [
{timestamp: {$gt: {$date: "2024-07-13T11:58:43.614+00:00"}}},
{error: {$regex: "Contains invalid characters"}}
]} )
< null
Null document is good as I do not have any document that matches.
May be you simply had a typing error when it failed.
@steevej $and is short-circuit, it’s probably failing on the timestamp and never reaches the $regex.
@Alfred_D_Souza I think your $regex is malformed. See https://www.mongodb.com/docs/manual/reference/operator/query/regex/
3 forms of $regex are listed there and your code seems to mix the forms and does not conform closely to any 1 of the 3:
{ <field>: { $regex: /pattern/, $options: '<options>' } }
{ "<field>": { "$regex": "pattern", "$options": "<options>" } }
{ <field>: { $regex: /pattern/<options> } }
E.g., you should try:
{error: {$regex: /Contains invalid characters/}}
or
{"error": {"$regex": "Contains invalid characters", "options": ""}}
steevej
(Steeve Juneau)
4
You are right. Thanks for remembering me. I must have been fooled with
without testing it myself. But after I did, it looks like $options is optional. The following does not generate any error with mongosh within Compass 1.43.4.
mongosh> d = db.c.findOne({error: {$regex: "Contains invalid characters"}} )
< null
All good points, @steevej … when debugging, I always recommend rigorous conformance first just to make sure … You tested it and proved your point!
I wonder how @Alfred_D_Souza is doing testing this?
1 Like
steevej
(Steeve Juneau)
6
In more than an occasion, I never hear back after I flagged a potential typo. I hope it will not be another one.
steevej
(Steeve Juneau)
7
@Alfred_D_Souza, as mentioned by Jack_Woehr, we wonder how you are testing that. We would appreciate a followup as we both spent our precious time reading, testing and writing about your issue.
thanks