I have two schema: one is the Zone:
var ZoneSchema = new mongoose.Schema({
name: String,
priceRate:Number,
...
})
the other is the zoneprice which ref to zone
var ZonePriceSchema = new mongoose.Schema({
zone:{type:ObjectId, ref:'Zone'},
})
now I want to do like that:
get ZonePirce where zonePrice.zone.priceRate>0 sort by priceRate desc skip(n) limit(m)
now I use two query but i think is ugly:
Zone.find({})
.where("priceRate").gt(0)
.sort({"priceRate":-1})
// .skip((pageNum-1)*pageSize)
// .limit(pageNum)
.exec(function(err,zones){
console.log("zones", zones.length);
if(err!=null) console.log(err);
zones.forEach(function(zone){
ZonePrice.find("zone",zone)
.sort({"price":-1})
.exec(function(err, zoneprices){
console.log("zoneprices", zoneprices.length);
zone.zoneprices = zoneprices;
})
});
retu zones;
}).then(function(zones){
console.log(zones.length);
res.json({
state:0,
zones:zones
});
})
})
are there any good ideas?
