Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The aggregator assists in localized sorting of MongoDB computing

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

For localized languages (for example, Chinese), MongoDB is sorted by UNICODE encoding, not by local language encoding. It is convenient to use esProc and MongoDB to sort localized languages (for example, Chinese is sorted by pinyin). Let's take Chinese as an example to take a look at the specific approach.

The collection person in MongoDB holds the name and gender as follows:

> db.person.find ()

{"_ id": ObjectId ("544e4e070f03ad39eb2bf498"), "name": "Song Jiang", "gender": "male"}

{"_ id": ObjectId ("544e4e070f03ad39eb2bf499"), "name": "Li Kui", "gender": "male"}

{"_ id": ObjectId ("544e4e070f03ad39eb2bf49a"), "name": "Wu Yong", "gender": "male"}

{"_ id": ObjectId ("544e4e070f03ad39eb2bf49b"), "name": "cover", "gender": "male"}

{"_ id": ObjectId ("544e4e070f03ad39eb2bf49c"), "name": "Gong Sun Sheng", "gender": "male"}

{"_ id": ObjectId ("544e4e070f03ad39eb2bf49d"), "name": "Lu Zhishen", "gender": "male"}

{"_ id": ObjectId ("544e4e070f03ad39eb2bf49e"), "name": "Wusong", "gender": "male"}

{"_ id": ObjectId ("544e4e070f03ad39eb2bf49f"), "name": "Ruan Xiaoer", "gender": "male"}

{"_ id": ObjectId ("544e4e070f03ad39eb2bf4a0"), "name": "Yang Zhi", "gender": "male"}

{"_ id": ObjectId ("544e4e070f03ad39eb2bf4a1"), "name": "Sun Erniang", "gender": "female"}

{"_ id": ObjectId ("544e4e070f03ad39eb2bf4a2"), "name": "bossy San Niang", "gender": "female"}

{"_ id": ObjectId ("544e4e080f03ad39eb2bf4a3"), "name": "Yanqing", "gender": "male"}

...

Directly use the sort function of MongoDB, not sorted by pinyin:

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

{"name": "Gongsun Sheng", "gender": "male"}

{"name": "Wu Yong", "gender": "male"}

{"name": "Sun Erniang", "gender": "female"}

{"name": "Song Jiang", "gender": "male"}

{"name": "bossy San Niang", "gender": "female"}

{"name": "Gai", "gender": "male"}

{"name": "Li Kui", "gender": "male"}

{"name": "Yang Zhi", "gender": "male"}

{"name": "Wusong", "gender": "male"}

{"name": "Yanqing", "gender": "male"}

{"name": "Ruan Xiaoer", "gender": "male"}

{"name": "Lu Zhishen", "gender": "male"}

...

The script to assist MongoDB with esProc is as follows:

A1: the connection MongoDB,ip and port number is localhost:27017, the database is test, and the user name and password are test. If you need other parameters, you can follow mongo://ip:port/db?arg=value&. Format continues to write parameters.

A2: use the find function to fetch numbers from MongoDB to form cursors. The collection is person, the filter condition is empty, and the specified keys are name and gender. You can see that the find function is similar to mongdb's find function. EsProc cursors read and process data in batches to avoid excessive amounts of data and memory overflows.

A3: because the amount of data is not large, all the records of fetch travel targets here.

A4: use sort to sort by name ascending order. The language used for sorting is Chinese. For other localized languages supported by the aggregator, see below.

The result of the operation is:

It is important to note that esProc does not contain MongoDB's java driver package. To access MongoDB with esProc, you must put the java driver package of MongoDB (version 2.12.2 or above, such as mongo-java-driver-2.12.2.jar) in [esProc installation directory]\ common\ jdbc in advance.

The script that esProc assists MongoDB calculation can be easily integrated into java. As long as you add a line of A5 and write it as result A4, you can output the results in the form of resultset to java. For specific code, please refer to the esProc tutorial. Similarly, using java to call esProc to access MongoDB must also put the java driver package of mongdb into the classpath of the java program.

The download address of the java driver package for MongoDB is https://github.com/MongoDB/mongo-java-driver/releases.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report