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 method of querying subscript in mongodb using SPL language of aggregator

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

The database Mongodb can not query the subscript through the element value and realize the ranking order in the array, but it can be realized by using the aggregator SPL language.

Rankings are popular in a variety of industries, ranging from world university rankings, Forbes rich lists to class student rankings, bank queuing for business, and so on. In the digital age, databases are usually more convenient to support ranking order. However, this is not the case for all databases, such as the database Mongodb, which can check the elements of an embedded array according to the subscript, but cannot query the subscript by the element value, that is, it cannot know the order of the elements. If you store people's names in the MongoDB array in order of rank, it can find names according to ranking (array subscript), but not by name lookup ranking (array subscript), that is, you can know who ranks, but you can't know who someone is ranked in. In this case, we can use the aggregator SPL language to help to perfectly realize the function of array subscript lookup, which is illustrated by an example below.

In the collection users of Mongodb, the name and friends (array) are saved. The names in the friends array are saved in order of ranking. The sample data are as follows:

> db.users.find ({"name": "jim"})

{

"_ id": ObjectId ("544f3bf8cdb02668db9ab229"), "name": "jim", "friends": ["tom", "jack", "luke", "rose", "james", "sam", "peter"]

}

Mongodb finds the names of the people in the specified rankings, such as the names of the people who rank first among jim's friends:

> db. Users.find ({"name": "jim"}, {"friends": {"$slice": [0mem1]}})

{

"_ id": ObjectId ("544f3bf8cdb02668db9ab229"), "name": "jim", "friends": ["tom"]

}

If we only use the MongoDB function, we can't find the ranking value of "luke" among jim's friends, but it can be easily achieved by using the SPL language.

The code to use the aggregator SPL is as follows:

A1=mongo_open ("mongodb://localhost:27017/local?user=test&password=test") 2=mongo_shell (A1, "users.find ({name:'jim'}, {name:1,friends:1,_id:0})") 3=A2.fetch () 4=A3.friends.pos ("luke") 5=mongo_close (A1)

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

A2: use the find function of MongoDB to fetch a number from the set users to form a cursor. The filter condition is name=jim, and the specified keys are name and friends. SPL cursors can read and process data in batches to avoid excessive amounts of data and memory overflows.

A3: because the amount of data is small, you can directly fetch all the records of the destination here.

A4: use the pos function to find out the location of luke.

The result of the operation is:

Value3

In short, SPL first obtains the ranking data through the conditions, and then uses the function pos () to find out the location of the given person's name, which is very easy to use and concise to write.

It is important to note that the aggregator SPL does not contain mongodb's java driver package. If you want to use SPL to access mongodb, you must place mongodb's java driver package (for example: mongo-java-driver-3.9.1.jar) in advance under the external library directory extLib\ MongoCli set by the aggregator.

In addition to calculating directly in the aggregator, the above script that uses the SPL language to assist mongodb calculation can also be easily integrated into java. As long as an additional line is added and written as return A4, you can output the results in the form of resultset to java. For specific code, please refer to the SPL tutorial. Similarly, using java to call SPL to access mongodb must also put the java driver package of mongdb into the classpath of the java program.

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