mongoose/mongodb custom sort

后端 未结 2 643
南方客
南方客 2021-01-25 23:47

I have a field in MongoDB that is a String type. It will contain some combination of letters and numbers like \"10\", \"101\", \"11\", \"112\", \"x115\", \"abc.5\". Right now

2条回答
  •  执念已碎
    2021-01-26 00:49

    You can use collation with numericOrdering parameter. Example:

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

    Here locale: 'en_us' is used for making a case insensitive search or sort.

提交回复
热议问题