MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

MongoDB:combina ricerca di testo e query geospaziale

Mongo DB stesso non supporta la ricerca di testo e geografica contemporaneamente.Fai riferimento a questo

Ma puoi combinare la query e ottenere il risultato

Soluzioni

  1. regex + vicino
const aggregate=YourCollection.aggregation()
aggregate.near({
    near:coord,
    includeLocs: "loc",
    distanceField: "distance",
    maxDistance: distance
});
aggregate.match({name:{$regex:keyword,$options: 'i'}})
aggregate.exec(function(err,yourDocuments){
    //your smart code
}) 
  1. Ricerca di testo + regex

    var aggregate=YourCollection.aggregate();
    var match= {
      latitude: { $lt: yourCurrentLatitude+maxDistance, $gt: yourCurrentLatitude-maxDistance },
      longitude: { $lt: yourCurrentLongitude+maxDistance, $gt: yourCurrentLongitude-maxDistance },
      {$text:{$search:keyword}}
    };
    
    aggregate.match(match).exec(function(err,yourDocuments){//your smart code})
    
  2. Mapreduce + ( Ricerca di testo Oppure regex )

YourCollection.mapReduce(map, reduce, {out: {inline:1, query : yourFirstQuery}}, function(err, yourNewCollection) {
// Mapreduce returns the temporary collection with the results
    collection.find(yourNewQuery, function(err, result) {
      //your awsome code
    });
});