Skip to content

Instantly share code, notes, and snippets.

@01speed1
Forked from diegoachury/index.js
Created January 31, 2017 23:23
Show Gist options
  • Save 01speed1/2c42ca3cdaf470f7e36c38747b715a7e to your computer and use it in GitHub Desktop.
Save 01speed1/2c42ca3cdaf470f7e36c38747b715a7e to your computer and use it in GitHub Desktop.
Buscador fulltext mongodb + express
// expressJS route
router.get('/search', function(req, res, next) {
Product.find({ $text: { $search: req.param('title') } } , function(err, docs){
res.render('shop/search', {products: docs} );
});
});
lo primero que realice fue uan busqueda en la documentacion de mongodb
https://docs.mongodb.com/v3.2/core/text-search-operators/
luego creo los index
aqui el ejemplo que me saco de la duda en la documentacion
Search for a Single Word
db.articles.find( { $text: { $search: "coffee" } } )
// modelo
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var schema = new Schema({
imagePath: {type: String, required: true},
title: {type: String, required: true},
});
schema.index({title: 'text'});
module.exports = mongoose.model('Product', schema);
<form action="" action="/search" method="get">
<input name="title" id="title" type="text" placeholder="realiza tu busqueda.">
<button type="submit" class="button postfix"><i class="material-icons">&#xE8B6;</i></button>
</form>
{{# each products }}
<p>{{ this.title }}</p>
{{/each}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment