Marc Stevens
Marc Stevens

Reputation: 363

MongoDB Database, order by a field which is a string field but with a number in it

I have a MongoDB and I need to order by a field (id), but this field is a string field with a number in it.

Example:

   {
     "_id": ObjectId("4e60f8c2b1638e5479c8ba6f"),
     "id": "3188",
     "type": "P"
   }

The id field with value "3188" I need to order on that field as if it's an integer. Has MongoDB got some magic that it can do this?

Thanks!

Upvotes: 3

Views: 1765

Answers (1)

Andrew Orsich
Andrew Orsich

Reputation: 53695

Magick of mongodb not enough to sort string as integer ;)

So there is only one solution is to make int field from string field and sort on it.

Upvotes: 2

Related Questions