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

后端 未结 6 547
花落未央
花落未央 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:28

    You can use

    db.collectionName.find().sort({title: 1}).collation({locale: "en_US", numericOrdering: true})
    

    numericOrdering flag is boolean and is Optional. Flag that determines whether to compare numeric strings as numbers or as strings. If true, compare as numbers; i.e. "10" is greater than "2". If false, compare as strings; i.e. "10" is less than "2". Default is false.

提交回复
热议问题