Answer the question
In order to leave comments, you need to log in
How to do an index search in Mongoose?
You need to do a full-text search on the table (mongoDB).
Went to off documentation.
Here is the diagram:
var mongoose = require('mongoose');
var testSchema = new mongoose.Schema({
tags: [],
text: { type: String }
});
testSchema.index({text: 'text', tags: 'text'});
module.exports = mongoose.model('Test', testSchema);
var test = require('../models/Test.js');
test
.find({ $text : { $search : "random" } })
.exec(function(err, results) {
if (err) return res.send(err);
res.send(results)
});
{
"_id" : ObjectId("57f3d21358b045bc1f16a156"),
"text" : "random 1",
"tags" : [ "a", "b", "c" ]
}
{
"_id" : ObjectId("57f3d2a858b045bc1f16a157"),
"text" : "test str 2",
"tags" : [ "d", "e", "f" ]
}
{
"_id" : ObjectId("57f3d2b758b045bc1f16a158"),
"text" : "random 2",
"tags" : [ "a", "e", "f" ]
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question