Skip to content

Instantly share code, notes, and snippets.

@romuloctba
Created December 17, 2016 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romuloctba/64fad52f4e37131f2010dee4e38f855d to your computer and use it in GitHub Desktop.
Save romuloctba/64fad52f4e37131f2010dee4e38f855d to your computer and use it in GitHub Desktop.
Text Search in Mongo DB / Busca de Texto no Mongodb
// You can only search text if you have text-search indexes. Easy Peasy.
// Você só pode buscar por texto se tiver um index (índice) de texto. Fácil pacas.
// ---
// To create a text index, run:
// Para criar um index, rode:
// (english example) db.collectionName.createIndex({ fieldName: "text" });
// (portuguese example) db.nomeDaCollection.createIndex({ nomeDoCampo: "text" });
// Ex: Collection "cars", fields "name", "model"
db.cars.createIndex({ name: "text", model: "text", brand: "text" });
// now you can search for text
db.getCollection('cars').find({ $text : { $search: "chevro" } });
// returns chevrolets car
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment