How to make a MongoDB query sort on strings with -number postfix?

后端 未结 6 555
花落未央
花落未央 2021-01-04 01:04

I have a query:

ownUnnamedPages = Entries.find( { author : this.userId, title : {$regex: /^unnamed-/ }}, {sort: { title: 1 }}).fetch()

That

6条回答
  •  星月不相逢
    2021-01-04 01:29

    MongoDB can't sort by numbers stored as strings. You either have to store the number as an integer in its own field, pad with leading zeroes, or sort the results after they've been returned from the database.

提交回复
热议问题