Reputation: 724
I want to be able to find all the pictures that contain a certain parameter, no matter which property contains that parameter, for example:
Say I type "John Snow" on the search bar, I'd like the result to be all pictures posted by John Snow (a person with that name), all pictures of John Snow (the character) and any picture containing that string in the description.
Is it possible to do this in the mongoose framework with node and express or is the answer to find by all possible properties and then remove duplicates?
var picSchema = new mongo.Schema({
name: String,
image: String,
description: String,
comments: [{
type: mongo.Schema.Types.ObjectId,
ref: "Comment"
}],
author: {
id: {
type: mongo.Schema.Types.ObjectId,
ref: "User"
},
username: String
}
});
Upvotes: 0
Views: 85
Reputation: 1009
For this purpose you could create and index:
picSchema.index({name: 'text', description: 'text', image: 'text'});
After node restart you would be able to perform search in the next way:
pic.find({ $text : { $search : "keyword" } }).exec((err, response)=> {});
Don't forget to register a model with your schema
Upvotes: 1