2016-09-30 53 views
0

我有3個系列:business,sevice,employee。Mongodb,在不同收藏中搜索文本和地理位置

我需要按服務搜索(無全文),按員工和每個業務的地理位置搜索,並且應該只顯示業務。

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集合,但是,如果我要同時搜索商業名稱,員工姓名和服務名稱,應該做出什麼樣的更改。

+0

你的問題不是很清楚。您想要優化的當前查詢是什麼?您可能在mongodb中查看'index' –

+0

搜索多個集合以獲得結果似乎效率不高,您認爲使用集合來僅搜索索引文本是一種很好的做法,該字段將會:name_business name_employee name_service,全部連接在一起。然後按照上一個查詢中找到的所有商戶中的位置進行過濾。 –

回答

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在業務schema.Then您您可以利用地理位置靠近MongoDB的查詢。