Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

node.js - Mongodb sort with case insensitive manner

I am struct very hard at one project in nodejs(express) with mongodb as database. When i get all data using sort() it returns data in wrong manner, so is there way to get it properly format as i am expecting as below: If we have three record in DB:

---------------------
id  | Name |  aga
---------------------
1   | atul | 21
---------------------
2   | Bhavik | 22
---------------------
3   | Jay | 25

What i am getting at present is:

2,3,1 series data

What i expect is to come is: 1,2,3

It means is to ignore the case while sorting is it possible without adding new column.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use collation here with locale: "en"

db.collection.find({}).collation({ locale: "en" }).sort({ name: 1 })

So for the below document

{ "_id" : 1, "name" : "Bhavik" }
{ "_id" : 2, "name" : "Jay" }
{ "_id" : 3, "name" : "atul" }

You will get

{ "_id" : 3, "name" : "atul" }
{ "_id" : 1, "name" : "Bhavik" }
{ "_id" : 2, "name" : "Jay" }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...