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

Assist in localized sorting of MongoDB computing

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Software localization allows users to choose different language versions according to their own language environment and usage habits, so as to maximize the use experience. With the progress of software technology, localization ability has been widely supported and developed continuously, which has become an important symbol of software maturity. The way of MongoDB localization ranking discussed in this article is also developed from scratch, accumulating little by little. Previous versions can only be sorted by UNICODE encoding, not by local language encoding. Later versions added support for localized sorting, but the premise is that the language is set when the collection is created, otherwise it is invalid, and it is not valid for collections that have already stored data. Through the operation of the aggregator SPL language combined with MongoDB, it is convenient to realize the sorting of the localized language (for example, sorting Chinese according to pinyin), and it is also very easy to implement. The following is illustrated with Chinese examples:

The collection person saves 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"}

...

You cannot sort by pinyin by directly using the sort function of MongoDB:

> 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 code to use the aggregator SPL is as follows:

A1: connect MongoDB, the connection word format is mongo://ip:port/db?arg=value& ….

A2: use the find function to fetch a number from person to form a cursor, the filter condition is empty, and the specified keys are name and gender. SPL 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: close the connection.

A5: use sort to sort by field name in ascending order of Chinese pinyin.

The result of the operation is as follows:

When sorting sort, it can be set to different languages according to the needs to achieve the sorting of query results. The aggregator SPL also supports other localized languages, as explained later.

It is important to note that the aggregator esProc does not contain mongodb's java driver package. To access mongodb with esProc, the java driver package of mongodb (for example: mongo-java-driver-2.12.2.jar) must be placed in advance under the external library directory extLib\ MongoCli set by the aggregator.

In addition to calculating directly in the aggregator, the above scripts that use SPL to assist mongodb calculation can also be easily integrated into java. As long as an additional line is added and written as return A5, you can output the results in the form of resultset to java. For specific code, 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 .

The languages supported by the aggregator SPL include:

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

Internet Technology

Wechat

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

12
Report