-1

I may have let a bug loose on our code that essentially ruined some of our db data.

Every now and again we come across a username that has " " in his email so we need to fix this.

How can I search for a value like "/"fubak@drooop.com"/" in one field using my mongo shell?

I have been trying all the combinations I can think of like:

db.users.find(username: /"/" .* /)

and

db.users.find(username: //.*//)

or

db.users.find(username: /"".*/)

But nothing seems to work.

ricardoespsanto
  • 1,000
  • 10
  • 34
  • do you mean `\"`? use a backslash to escape the `"`. – Simulant May 15 '14 at 10:09
  • the problem is that the fields already have quotes. so a normal search like: db.user.find() will return {username: "my.username@bla.com"} so what you proposed will match everyone – ricardoespsanto May 15 '14 at 10:16

3 Answers3

0

Not to sure which one is you problem, so the solutions to both possible scenarios with data in MongoDB like this:

{ "email" : "\"/\"fubak@drooop.com\"/\"" }
{ "email" : "/fubak@drooop.com/" }
{ "email": "\"blablabla@blip.ca\"" }

First document match:

db.problem.find({ email: /^\"/  })

Second document match:

db.problem.find({ email: /^\// })

Third document match

db.problem.find({ email: /^\"/ })

Do not know what you mean otherwise by "having quotes" as that is not valid JSON and by extension is also invalid BSON.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • that didn't work. My values are exactly stored as "username" : "\"blablabla@blip.ca\"" and I need it to be "username" : "blablabla@blip.ca" – ricardoespsanto May 15 '14 at 10:31
  • @Ricky okay then. Does the last one match. It does for me. And notice the "caret" `^` because your MongoDB performance will be terrible without that. – Neil Lunn May 15 '14 at 10:43
  • I've fixed this with RegExp (already answered) and sorry when I tried your solution I may have left the field name as email which was wrong so that might have been why it didn't work – ricardoespsanto May 15 '14 at 10:46
  • 1
    @Ricky the point I have been generally making ( outside of the actual regex required, but the last example should suit ) is that if you do not "anchor" the the caret `^` then your MongoDB performance drops considerably. This may be a "get out of jail" operation for you specifically, but in general, un-bounded Regex operations in MongoDB are bad news. I just want people to be aware of that. – Neil Lunn May 15 '14 at 10:57
  • you are absolutely right! searching the beginning of a string is soo much faster than searching it all! – ricardoespsanto May 15 '14 at 11:07
  • @Ricky some people might deem that information useful or even as the answer resolves your problem an acceptable answer. But that is entirely up to you. – Neil Lunn May 15 '14 at 11:11
0

new RegExp('^".*')

This finds all the records that start with ".

Thanks.

ricardoespsanto
  • 1,000
  • 10
  • 34
0

Try this:

db.users.find({ email: "(?:[^\\"]+|\\.)*  })   

This will match all documents which contains quotes in the email

mohamedrias
  • 18,326
  • 2
  • 38
  • 47