Last active
January 31, 2017 23:23
-
-
Save diegoachury/59b19dac89c273d5ae33db53a91ced8b to your computer and use it in GitHub Desktop.
Buscador fulltext mongodb + express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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} ); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } } ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment