Sajeetharan
Sajeetharan

Reputation: 222692

Filter by country and free text search mongodb using mongoose

I have document in mongodb as follows,

enter image description here

My schema is as follows,

var mongoose = require('mongoose');
var eventSchema = new mongoose.Schema({
    description: {
        type: String        
    },
    end_time: {
        type: Date      
    },
    start_time: {
        type: Date      
    },
    name: {
        type: String    
    },
    place: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Location'
    }
});
eventSchema.index({description: 'text'});
module.exports= Event;

I am trying to query the documents where country is "Australia" and includes text in the description "Ade". but the following query returns empty result set, whereas there are around 15 documents.

query : function(model, conditon,options) {
        return new Promise(function(resolve, reject) {
            options = options||{};
            console.log("model is" + model);
            model.find(conditon, {}, options, function(error, data) {
                if (error)
                    console.log(error);
                    reject(error);               
                resolve(data);
            })
        })
    }

This is how i call the query,

dbHelper.query(mongoose.model('events'), {$text: {$search: "Ade"},'place.location.country': "Australia"},function(error,data){
                        callback(data);       
});

Upvotes: 0

Views: 1514

Answers (1)

Jyotman Singh
Jyotman Singh

Reputation: 11340

First of all I hope you know that to perform $text based queries you need to have a text based index on that particular field.

Secondly you seem to be confusing callbacks and promises -

As I can see your query() function is returning a promise still when you're calling the query() function you're expecting the callback. Promises are returned immediately to you and then you need to resolve them.

Your code should look like this -

dbHelper.query(mongoose.model('events'), {$text: {$search: "Ade"},'place.location.country': "Australia"})
.then(function(result)){
    // you have your data here
}
.catch(function(err)){
    // an error occured
}

Read more about Promises here.

Another minor bug I can notice is in the query() method. The if statement does not use curly brace and as a result only the very next statement after the if statement will be executed under the if clause. The second statement after the if will always be executed -

So,

if(error)
    console.log(error);
// always executed
reject(error);
resolve(data);

should be -

if(error){
    console.log(error);
    reject(error);
}
resolve(data);

I'm still not sure all these would be able to make your code work as I can't see the whole picture here. I IMHO recommend that you invest some more time covering the javascript and MongoDB basics. Will help you save a lot of time. And for that matter MongoDB's official docs are really good.

Upvotes: 2

Related Questions