art
art

Reputation: 306

Search string with special characters in mongo

I want to search a string in mongo which is stored with special characters

db.getCollection('tb_tickets').find({"workflow_fields.label": "3~!@#$%^&*()__+}{|":<>?/.,';\][NAME"})

My use case doesn't allow me to first escape the special characters and then store in mongo. How can I search 3~!@#$%^&*()__+}{|":<>?/.,';\][NAME in mongo query.

Upvotes: 0

Views: 406

Answers (1)

klhr
klhr

Reputation: 3380

You need to escape the quotation mark: "3~!@#$%^&*()__+}{|\":<>?/.,';\][NAME" (note the \ before the ")

> db.test_coll.insert({string: `3~!@#$%^&*()__+}{|":<>?/.,';\][NAME`});
WriteResult({ "nInserted" : 1 })
> db.test_coll.find({string: "3~!@#$%^&*()__+}{|\":<>?/.,';\][NAME" });
{ "_id" : ObjectId("5cdafcb1176f787fae2db871"), "string" : "3~!@#$%^&*()__+}{|\":<>?/.,';][NAME" }

(If you take a look at your question, you can tell that something is going awry because the syntax highlighting isn't correctly highlighting your string.)

Upvotes: 1

Related Questions