Abhijeet Abhijeet - 4 months ago 10
Node.js Question

Find all the documents having * in them in MongoDB

Can someone please help me with the regex for * in MongoDB.
I want to fetch all the documents having '*' in them.

like that regex (db.collection.find{value: new Regex('*')}) should return all such documents

{"id":1,value:"abc/123/*"}
{"id":2,value:"abc/*/lmn"}
{"id":3,value:"abc*lmn"}

Answer

You're close; you just need to add a backslash to escape the * in your regex so it won't be treated as a special character.

In the shell:

db.collection.find({value: /\*/})
Comments