ist eine sehr häufige Anforderung in der geo-Filter und Textsuche in einem Anwendungsfall erforderlich ist, die leider direkt noch nicht von mongodb unterstützt.
Unten Code verwendet Mungo-Treiber, filtert Dokumente zuerst basierend auf Standort (Länge und Breite) und dann weitere Filter basierend auf dem gesuchten Begriff.
var area = {
center: [51, -114], //lng = 51 and lat = -114
radius: 100,
unique: true //this option is deprecated from MongoDB 2.6 on as mongodb no longer returns duplicate results
};
var query = Post.where('loc').within().circle(area) //Step 1: filter based on location
//Step 2: Next filter on the basis of searched text
.where({
$text: {
$search: <searchTerm>
}
}, {
score: {
$meta: 'textScore'
}
})
//Step 3: Filter any document fields that should not be returned in the result
.select({
"val1": 0,
"val2": 0
});
//Execute the query
query.exec(function (err, result) {
if (err) {
//return error in the response
}
//return result object in the response
});
In diesem Code "Post" ist ein Mungo-Schema wie etwas unter
var PostSchema = new Schema({
title: String,
description: String,
loc: {
type: [Number], // [<longitude>, <latitude>]
index: '2d' // create the geospatial index
}
//some other fields
}
module.exports = mongoose.model('Post', PostSchema);
Auch für den Suchbereich, gibt es andere wie Box verfügbaren Optionen
var lowerLeft = [40.73083, -73.99756]
var upperRight= [40.741404, -73.988135]
query.where('loc').within().box(lowerLeft, upperRight)
Sowohl für die Geolokalisierung Suche und Textsuche zu arbeiten, stellen Sie sicher, dass Index auf Loc-Feld und Textfeld. Weitere Details hier. loc Search
kann ich $ lt und $ gt Betreiber verwenden, wenn die Breite und Länge in einem Array gespeichert werden ; etwas wie- loc: [50.433234,20,220123] –