MyPage is a personalized page based on your interests.The page is customized to help you to find content that matters you the most.

I'm not curious

MongoDB - Sorting in Local Language

Published on 02 December 14

MongoDB uses unicode, instead of the coding for a certain local language, to sort data in this language (i.e. Chinese). Together with esProc, MongoDB can realize sorting in local language conveniently (i.e. sort Chinese according to Chinese phonetic alphabet). The following will teach you the method in detail by taking Chinese as an example.

person - a collection in MongoDB - stores names and genders as follows:

> db.person.find()

{ "_id" : ObjectId("544e4e070f03ad39eb2bf498"), "name" : "å®æ±", "gender" : "ç·"}

{ "_id" : ObjectId("544e4e070f03ad39eb2bf499"), "name" : "æéµ", "gender" : "ç·"}

{ "_id" : ObjectId("544e4e070f03ad39eb2bf49a"), "name" : "å´ç¨", "gender" : "ç·"}

{ "_id" : ObjectId("544e4e070f03ad39eb2bf49b"), "name" : "æç", "gender" : "ç·"}

{ "_id" : ObjectId("544e4e070f03ad39eb2bf49c"), "name" : "å¬å­è", "gender" : "ç·" }

{ "_id" : ObjectId("544e4e070f03ad39eb2bf49d"), "name" : "é²æºæ·±", "gender" : "ç·" }

{ "_id" : ObjectId("544e4e070f03ad39eb2bf49e"), "name" : "æ­¦æ¾", "gender" : "ç·"}

{ "_id" : ObjectId("544e4e070f03ad39eb2bf49f"), "name" : "é®å°äº", "gender" : "ç·" }

{ "_id" : ObjectId("544e4e070f03ad39eb2bf4a0"), "name" : "æ¨å¿", "gender" : "ç·"}

{ "_id" : ObjectId("544e4e070f03ad39eb2bf4a1"), "name" : "å­äºå¨", "gender" : "女" }

{ "_id" : ObjectId("544e4e070f03ad39eb2bf4a2"), "name" : "æä¸å¨", "gender" : "女" }

{ "_id" : ObjectId("544e4e080f03ad39eb2bf4a3"), "name" : "çé", "gender" : "ç·"}


Sort the data using MongoDBâs sort function rather than the Chinese phonetic alphabet:

> db.person.find({},{"name":1,"gender":1,"_id":0}).sort({"name":1})

{ "name" : "å¬å­è", "gender" : "ç·" }

{ "name" : "å´ç¨", "gender" : "ç·" }

{ "name" : "å­äºå¨", "gender" : "女" }

{ "name" : "å®æ±", "gender" : "ç·" }

{ "name" : "æä¸å¨", "gender" : "女" }

{ "name" : "æç", "gender" : "ç·" }

{ "name" : "æéµ", "gender" : "ç·" }

{ "name" : "æ¨å¿", "gender" : "ç·" }

{ "name" : "æ­¦æ¾", "gender" : "ç·" }

{ "name" : "çé", "gender" : "ç·" }

{ "name" : "é®å°äº", "gender" : "ç·" }

{ "name" : "é²æºæ·±", "gender" : "ç·" }


The esProc script helping with MongoDB computation is as follows:
MongoDB - Sorting in Local Language  - Image 1

A1ï¼Connect to the MongoDB database. The IP and port number is localhost:27017, the database name is test and both the user name and the password are test. If any other parameters are needed, write them in line with the format mongo://ip:port/db?arg=value&â¦

A2ï¼Fetch data from the MongoDB database using find function to create a cursor. The collection is person. The filtering criterion is null and the specified keys are name and gender. It can be seen that this find function is similar to the find function of MongoDB. By fetching and processing data in batches, the esProc cursor can avoid the memory overflow caused by big data importing.

A3ï¼Since the data here are small, fetch function will fetch them all at once.

A4ï¼Sort the data by name in ascending order, using sort function. Chinese is used in the data sorting. For the other localized languages esProc supports, please see below.

The result of operation is:
MongoDB - Sorting in Local Language  - Image 2

One thing to note is that esProc doesnât provide the java driver of MongoDB. To access MongoDB with esProc, the latter (a driver of 2.12.2 version or above is required, i.e. mongo-java-driver-2.12.2.jar) should be put into the [esProc installation directory]\common\jdbc beforehand.

The script for computation in MongoDB with the assistance of esProc is easy to integrate with Java program. By adding another line of code - A5, which is result A4, the result in the form of resultset can be output to Java program. For detailed code, please refer to esProc Tutorial. In the same way, to access MongoDB by calling esProc code with Java program also requires putting the java driver of MongoDB into the classpath of Java program.

The java driver of MongoDB can be downloaded from the following URL: .

More information : .
This blog is listed under Open Source , Development & Implementations and Data & Information Management Community

Related Posts:
Post a Comment

Please notify me the replies via email.

  • We hope the conversations that take place on will be constructive and thought-provoking.
  • To ensure the quality of the discussion, our moderators may review/edit the comments for clarity and relevance.
  • Comments that are promotional, mean-spirited, or off-topic may be deleted per the moderators' judgment.
You may also be interested in
Awards & Accolades for MyTechLogy
Winner of
Top 100 Asia
Finalist at SiTF Awards 2014 under the category Best Social & Community Product
Finalist at HR Vendor of the Year 2015 Awards under the category Best Learning Management System
Finalist at HR Vendor of the Year 2015 Awards under the category Best Talent Management Software
Hidden Image Url