0

I'm creating mongodb queries dynamically but when I use $regex it throws an exception. The query works if I use it directly in the shell!

Any suggestions? I really need to use dynamic queries.

method: this.Collection.Find(query).Limit(limit).ToListAsync(cancellationToken);

query: { $and: [ { Name: { '$regex': /batman/i } } ] }

exception:'BsonSerializer.Deserialize<BsonDocument>("{ $and: [ { Name: { '$regex': /batman/i } } ] }")' threw an exception of type 'System.FormatException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233033
HelpLink: null
InnerException: null
Message: "JSON reader expected a string but found '/batman/i'."
Source: "MongoDB.Bson"

1 Answer 1

1

You can try separating the options from the search value.

{ $and: [ { Name: { '$regex': 'batman', '$options' : 'i' } } ] }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.