node.js - Moongoose sort by parseFloat(String) -


i want sort query result float value. value stored in mongodb type string,can parse string float , sort dynamically? complex sort.

the following parts of schema , sort code:

schema:

var scenicspotschema = new schema({     ...     detail_info: {         ...         overall_rating: string,         ...     }, }); 

sort function:

    scenicspot.find({'name': new regexp(req.query.keyword)}, )         .sort('-detail_info.overall_rating')         .skip(pagesize * pagenumber)         .limit(pagesize)         .exec(function (err, scenicspots) {             if (err) {                 callback(err);             } else {                 callback(null, scenicspots);             }         }); 

any kind of , advice appreciated. :)

.sort mongoose not support converting data type. see: http://mongoosejs.com/docs/api.html#query_query-sort

it accept column names , order.

there 2 path acheive goal:

  1. use mapreduce in mongo, first convert type, , sort
  2. retrieve data database, , sort in node.js program.

but both terrible , ugly

if wanna sort string column parse float. action scan data in collection, , can not use index. it's very slow action.

so think fastest , correct operation convert string column float in mongodb database. , can use normal .sort('-detail_info.overall_rating') things done.


Popular posts from this blog