I'm facing a strang problem while sorting documents in mongoose.
I have a huge scheme named as Property that contain a price object something like this:
{
info: {
price: {
price: 361000 // number
}
}
}
Property Model:
@Field(() => Number, { nullable: true })
@Prop({ type: SchemaTypes.Number })
price: number;
Now, I have to sort the properties via property price i.e info.price.price.
What I have done:
const properties = await this.propertiesModel
.find({})
.skip(skipBy)
.limit(size)
.sort({ "info.price.price": -1 }); // sorting
The results are not as expected:
{
"info": {
"price": {
"price": 999000
}
}
},
{
"info": {
"price": {
"price": 99900 // wrong
}
}
},
{
"info": {
"price": {
"price": 997000
}
}
}
The schema is already using the Number type for price but seems like it's sorting as a string somehow.