I hope some one can help me - i want to populate a subdocument with a geo query to sort them:
i've got these models (simplified):
(a lot of articles):
var articleSchema = mongoose.Schema({
places: [{ type: mongoose.Schema.Types.ObjectId, ref: "places", required: false }],
someData: { type: String, required: true },
})
(a lot of places):
var placeSchema = mongoose.Schema({
longitudelatitude: {
type: { type: String, required: true },
coordinates: [
{ type: Number, required: true }, //longitude
{ type: Number, required: true } //latitude
]},
someData: { type: String, required: true }
})
my first query to find only places near a Position works fine:
getPlacesNearBy: function (lng, lat, skipNumber, limitNumber, callback) {
Place.find({
longitudelatitude: {
$near: {
$geometry: { type: "Point", coordinates: [lng, lat] },}}
}, null, {skip: skipNumber, limit: limitNumber}, function (err, foundPlaces) {
if (err)
return callback(err, null);
return callback(null, foundPlaces);
})
},
i get places near me - i can choose with limit how many - and i can reload some more with skip
now i wanted to do something similar:
i want to get an article - and populate the stored places (where you can get them) AND AFTER THAT i want to sort the places by distance and maybe to skip or limit the Response
so i tryed:
getPlacesforArticle: function (articleId, lng, lat, skipNumber, limitNumber, callback) {
var projection = null;
Article.findById(articleId, {places: 1}).populate("places", projection, {
longitudelatitude: {
$near: {
$geometry: { type: "Point", coordinates: [lng, lat] },
}
}
}, {skip: skipNumber, limit: limitNumber}).exec(function (getError, foundArticle) {
if (getError)
return callback(getError, null);
callback(null, foundArticle.places);
});
}
},
So this query is working (throw no error) but it doesnt response what i want - i get places but they are "ordered" by database sequenze not by distance - so the $near seems to be ignored (tested without this query brings same result) but when i filter to some other content that works fine (like Name="test") also the limit is working but i cant skip some places of this response...
well i hope some one understand me and can help me =)!
thank you very much!