How to overcome lexical sorting in mongodb

While sorting an array of objects on multithreads, I ran into a lexical sorting problem. Please suggest how to overcome them.

My array looks like this:

var arr=[{
    "type" : "initials",
    "left" : "430.078125px",
    "top" : "220px",
    "height" : "32px",
    "width" : "124px",
    "role" : "Volunteer",
    "template_id" : "06664dfe-6288-4c1f-aff6-b4a093a61762",
    "onpage" : "1",
    "_id" : "c20f6d59-c648-4d41-97cb-19b8650adb37",
    "created_user_id" : "249f63e7-0aa6-4ea4-baf1-3c8d06dbcdc9",
    "created_at" : "2014-11-18T15:22:56+05:30",
    "modified_at" : "2014-11-18T15:22:56+05:30",
    "modified_user_id" : "249f63e7-0aa6-4ea4-baf1-3c8d06dbcdc9",
    "status" : "A"
},

{
    "type" : "text",
    "left" : "335.5px",
    "top" : "46px",
    "height" : "30px",
    "width" : "72px",
    "fieldname" : "freshman",
    "role" : "Volunteer",
    "template_id" : "06664dfe-6288-4c1f-aff6-b4a093a61762",
    "onpage" : "1",
    "_id" : "f64ca1f2-22b2-4000-94a0-f23264fd2bca",
    "created_user_id" : "249f63e7-0aa6-4ea4-baf1-3c8d06dbcdc9",
    "created_at" : "2014-11-18T15:22:56+05:30",
    "modified_at" : "2014-11-18T15:22:56+05:30",
    "modified_user_id" : "249f63e7-0aa6-4ea4-baf1-3c8d06dbcdc9",
    "status" : "A"
},

{
    "type" : "text",
    "left" : "110.5px",
    "top" : "101px",
    "height" : "31px",
    "width" : "163px",
    "fieldname" : "name",
    "role" : "Volunteer",
    "template_id" : "06664dfe-6288-4c1f-aff6-b4a093a61762",
    "onpage" : "1",
    "_id" : "7343dd52-2955-4a7a-924c-ac2edf84412f",
    "created_user_id" : "249f63e7-0aa6-4ea4-baf1-3c8d06dbcdc9",
    "created_at" : "2014-11-18T15:22:56+05:30",
    "modified_at" : "2014-11-18T15:22:56+05:30",
    "modified_user_id" : "249f63e7-0aa6-4ea4-baf1-3c8d06dbcdc9",
    "status" : "A"
}]

      

My request is like this collection.find().sort({"onpage":1,"top":1,"left":1})

After that I get results like

[
{
    "type" : "text",
    "left" : "110.5px",
    "top" : "101px",
    "height" : "31px",
    "width" : "163px",
    "fieldname" : "name",
    "role" : "Volunteer",
    "template_id" : "06664dfe-6288-4c1f-aff6-b4a093a61762",
    "onpage" : "1",
    "_id" : "7343dd52-2955-4a7a-924c-ac2edf84412f",
    "created_user_id" : "249f63e7-0aa6-4ea4-baf1-3c8d06dbcdc9",
    "created_at" : "2014-11-18T15:22:56+05:30",
    "modified_at" : "2014-11-18T15:22:56+05:30",
    "modified_user_id" : "249f63e7-0aa6-4ea4-baf1-3c8d06dbcdc9",
    "status" : "A"
},
{
    "type" : "initials",
    "left" : "430.078125px",
    "top" : "220px",
    "height" : "32px",
    "width" : "124px",
    "role" : "Volunteer",
    "template_id" : "06664dfe-6288-4c1f-aff6-b4a093a61762",
    "onpage" : "1",
    "_id" : "c20f6d59-c648-4d41-97cb-19b8650adb37",
    "created_user_id" : "249f63e7-0aa6-4ea4-baf1-3c8d06dbcdc9",
    "created_at" : "2014-11-18T15:22:56+05:30",
    "modified_at" : "2014-11-18T15:22:56+05:30",
    "modified_user_id" : "249f63e7-0aa6-4ea4-baf1-3c8d06dbcdc9",
    "status" : "A"
},

{
    "type" : "text",
    "left" : "335.5px",
    "top" : "46px",
    "height" : "30px",
    "width" : "72px",
    "fieldname" : "freshman",
    "role" : "Volunteer",
    "template_id" : "06664dfe-6288-4c1f-aff6-b4a093a61762",
    "onpage" : "1",
    "_id" : "f64ca1f2-22b2-4000-94a0-f23264fd2bca",
    "created_user_id" : "249f63e7-0aa6-4ea4-baf1-3c8d06dbcdc9",
    "created_at" : "2014-11-18T15:22:56+05:30",
    "modified_at" : "2014-11-18T15:22:56+05:30",
    "modified_user_id" : "249f63e7-0aa6-4ea4-baf1-3c8d06dbcdc9",
    "status" : "A"
}] 

      

I want top 46px on top. I am using node.js. How to overcome this issue in mongodb?

+3


source to share


1 answer


Store your data as numbers, not as strings (e.g. 220

instead of "220px"

), if a part px

is important to you, store it in a separate field, for example "measurement_unit": "px"

).



MongoDB strings can only be sorted lexically.

+4


source







All Articles