Mongodb,搜索不同集合中的文本和地理位置

时间:2016-09-30 01:41:45

标签: node.js mongodb mongoose

我有3个收藏:业务,服务,员工。

我需要按服务搜索(没有全文),按员工和每个商家的地理位置进行搜索,并且只能显示商家。

var BusinessSchema = new Schema({   
        business_id: {
            type: String,
            required: true,
            unique:true
        },
        name: {
            type: String,
            required: true
        },
        email: {
            type: String,
        },      
        description:{
            type: String
        },
        location:{
            country:{
                type:String,
            },
            city:{
                type:String
            },
            coord:[Number]
        }       
        services:[{
            type: Schema.Types.ObjectId,
            ref: 'Service'
        }]
    },
    {
        timestamps: true
    });


var ServiceSchema = new Schema({
        business:{
            type: Schema.Types.ObjectId,
            ref: 'Business'
        },      
        category:{
            type: Schema.Types.ObjectId,
            ref: 'Category',
            index:true
        },
        name: {
            type: String,
            required: true,
            index:true
        },
        employee: [{
            type: Schema.Types.ObjectId,
            ref: 'User'
        }], 
        {
        timestamps: true
    });

var UserSchema = new Schema({
        birthday:Date,

        first_name: {
            type: String,
            required: true
        },
        last_name: {
            type: String,
            required: true
        },
        email: {
            type: String,
            unique: true,
            match: [/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/, 'Please fill a valid email address'],
            required: true
        },
        password:{
            type: String
        },
        {
        timestamps: true
    });

我应该对集合进行哪些更改以优化查询?

Service.distinct('business',filter_services)
        .exec(function (err, business) {
            if (err) {
                return cb({ status: 400, message: err }, null);
            } else {
                if(business.length > 0){
                    var filter_business = [{is_active:true},{is_approved:true}]
                    filter_business.push({_id:{$in:business}})
                    filter_business.push({location.coord:input_coord}})
                    filter_business = {$and:filter_business}

                    Business.find(filter_business)
                    .select('name services')
                    .exec(function (err,result){
                        if(err){
                            return cb({ status: 400, message: err }, null);
                        }
                        else{
                            if(result.length > 0){
                                var total = result.length;

                            }
                            return cb(null, result);
                        }
                    })
                }
                // si no hay business en el primer query, se retorna [].
                else{
                    return cb(null, business);
                }

            }            
        });

可以按文字进行地理过滤,同时更接近某个点吗?

目前,我没有使用Employee集合,但是,如果我同时搜索公司名称,员工姓名和服务名称,应该做出哪些更改。

1 个答案:

答案 0 :(得分:0)

您的商业模式应该是

    var BusinessSchema = new Schema({   
            business_id: {
                type: String,
                required: true,
                unique:true
            },
            name: {
                type: String,
                required: true
            },
            email: {
                type: String,
            },      
            description:{
                type: String
            },
            address:{
                country:{
                    type:String,
                },
                city:{
                    type:String
                }                
            },
            location : {
                type: [Number],
               index: '2dsphere'
            },       
            services:[{
                type: Schema.Types.ObjectId,
                ref: 'Service'
            }],
            employees:[{
                type: Schema.Types.ObjectId,
                ref: 'User'
            }]
        },
        {
            timestamps: true
        });

架构的变化是您必须在位置上创建索引,而基于员工的查找业务必须在业务架构中添加员工ID。然后您可以在mongodb附近查询地理位置。