The digital comparison of the Collector assist MongoDB calculation

Source: Internet
Author: User
Tags diff

MongoDB Scripting has limited computational power to solve complex problems, and is more difficult to use directly. In many cases, it is necessary to read the data out of the main program to complete the operation, and in high-level languages such as Java to write this kind of aggregate operation is also more troublesome. This can be aided by the esproc of the collector, and the use of this method is illustrated by examples.

There is a test set in MongoDB as follows:

>db.test.find ({},{"_id": 0})

{"Value": Numberlong (112937552)}

{"Value": Numberlong (715634640)}

{"Value": Numberlong (487229712)}

{"Value": Numberlong (79198330)}

{"Value": Numberlong (440998943)}

{"Value": Numberlong (93148782)}

{"Value": Numberlong (553008873)}

{"Value": Numberlong (336369168)}

{"Value": Numberlong (369669461)}

...

Specific: The test collection contains more than one value, and each value is a string of numbers. Each number string is compared to all the other numbers, finding the maximum number of identical and maximum different numbers for each number string. If the 1th and nth rows contain the number 1, even if there are 1 of the same number, if the 1th row has 1 nth rows, there are 1 of the different numbers.

The Collector code is as follows:

A1: The connection Mongodb,ip and the port number are localhost:27017, the database is test, and the user name and password are test.

A2: Use the Find function to take a number from MongoDB to form a cursor. The collection is test, the filter condition is NULL, the key _id not taken out. As you can see, Esproc uses the same parameter format as the MONGDB find statement in the Find function. Esproc Cursors Support batch reading and processing of data to avoid excessive data volume and memory overflow. This is because the amount of data is not large, so fetch all records of the marked.

A3: Add two columns on A2 basis to hold the maximum number of identical and maximum different numbers. At the same time, the value is converted to a string.

A4: For A3 collection loops, the loop body is B4 to D10.

B4: Takes the value of the current loop.

C4: Use a function to split value into a sequence of single characters, removing duplicate values.

B5: One more inner loop for the A3. The loop body is C6 to D10.

C5: If the loop position of the inner loop is equal to the current position of the outer layer, that is, the same value, the inner loop is skipped and the next inner loop is performed.

C6: Gets the value of the inner loop.

C7: Defines two variables same and diff, each storing the same number and different number of this comparison, the initial value is 0.

C8: Use the loop function to find the sequence value of the value split of the outer loop one by one in value in the inner loop. If it can be found, then same add 1, otherwise diff will add 1.

C9, C10: Compares Same and diff in same and diff with A4, and assigns a large re-assignment to A4 and diff in same.


The operating result is:

It is necessary to note that Esproc does not contain MongoDB's Java driver package. To access MongoDB using Esproc, the MongoDB Java driver Package (required 2.12.2 or above, such as Mongo-java-driver-2.12.2.jar) must be placed in advance to the [Esproc installation directory]\COMMON\JDBC.

Esproc assist MongoDB to calculate the script is easy to integrate into Java, as long as adding a line of A11, written as result A3 can output to Java resultset form of results, specific code reference Esproc tutorial. Similarly, using Java to invoke Esproc to access MongoDB must also place the MONGDB Java driver package in the Java program's classpath.


The digital comparison of the Collector assist MongoDB calculation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.