Lets say I have a Mongoose model like this:
var AdSchema = new mongoose.Schema({
vehicleDescription: String,
adDescription: String,
...
});
I want to search both fields for the same key word, for example
find all WHERE vehicleDescription contains the word TEST, OR adDescription contains the word TEST.
It needs to be case sensitive also.
Currently I can query one field like this:
var query = {};
query['adDescription'] = ({ $regex: search.keyWord, $options: 'i' });
But I need to search both fields at the same time.
Thanks.
You can search for more than one fields at the same time by specifying the field and value in a {field 1: value 1}, {field 2: value 2},...,{field n: value n}
format in the find()
function. Additionally, you can also specify various conditions that the results must satisfy, such as AND, OR etc.
In your case, a sample code in Node.js would look like this
var result = db.collection('AdSchema').find({
$or: [ {vehicleDescription : { $regex: search.keyWord, $options: 'i' }}, { adDescription: { $regex: search.keyWord, $options: 'i' } } ]
});
For more detail, refer the documentation page for MongoDB.