0

I have a collection with the following documents

1.question:"Best OS?"
2.question:"Best Mobile?"
3.question:"Worst OS?"

If i search for a string "Best" then it results then it must return 1 and 2 documents. How to achieve this in mongo

Sasikanth
  • 3,045
  • 1
  • 22
  • 45
  • possible duplicate of [Checking if a field contains a string](http://stackoverflow.com/questions/10610131/checking-if-a-field-contains-a-string) – WiredPrairie Feb 17 '14 at 13:10
  • what if i want to search all fields in a document??? – Sasikanth Feb 17 '14 at 13:13
  • You're changing the question now. That too has been covered dozens of times already. You'd need to use the beta text search feature of MongoDb or change the structure to put all values and keys in a new structure. Honestly, I'd suggest using a full text search engine. It's more more suited to this task than MongoDb. – WiredPrairie Feb 17 '14 at 13:20
  • Duplicate example: http://stackoverflow.com/questions/8238181/mongodb-how-to-find-string-in-multiple-fields – WiredPrairie Feb 17 '14 at 13:21

1 Answers1

0

Use the $regex operator:

db.collection.find( { question: /Best*/ } );
Agis
  • 32,639
  • 3
  • 73
  • 81
  • 1
    Might help to add that searching **anywhere** in the string is going to be bad for performance. And perhaps suggest some alternatives. – Neil Lunn Feb 17 '14 at 08:12