使用 mongoose 按国家和自由文本搜索 mongodb 过滤 [英] Filter by country and free text search mongodb using mongoose

查看:36
本文介绍了使用 mongoose 按国家和自由文本搜索 mongodb 过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mongodb中有如下文档,

I have document in mongodb 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;

我正在尝试查询国家为澳大利亚"并在描述Ade"中包含文本的文档.但以下查询返回空结果集,而大约有 15 个文档.

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);       
});

推荐答案

首先,我希望您知道要执行基于 $text 的查询,您需要有一个 该特定字段的基于文本的索引.

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 -

正如我所看到的,当您调用 query() 函数时,您的 query() 函数仍在返回一个承诺,您期待回调.Promise 会立即返回给您,然后您需要解决它们.

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
}

阅读有关 Promises 的更多信息 这里.

Read more about Promises here.

我注意到的另一个小错误是在 query() 方法中.if 语句 不使用大括号,因此只有 if 语句 之后的下一个语句将在 if 子句下执行.if 之后的第二条语句会一直执行 -

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 -

所以,

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

应该是 -

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

我仍然不确定所有这些是否能让您的代码正常工作,因为我在这里看不到全貌.恕我直言,我建议您投入更多时间来了解 javascript 和 MongoDB 基础知识.将帮助您节省大量时间.就此而言,MongoDB 的官方文档 非常好.

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.

这篇关于使用 mongoose 按国家和自由文本搜索 mongodb 过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆