The collector computes the array of MongoDB to check the subscript

Source: Internet
Author: User

Mongodb can follow the subscript to isolate the elements of an inline array, but cannot query the subscript by the value of the element. For example, the elements in the array are the names of the people in the order of rank, andMongoDB can find names based on the rank (array subscript), but cannot find the rank (array subscript) value by name. Esproc can assist MongoDB to achieve this requirement, as shown in the following example.

In the Mongodb collection b , the names and friends (arrays) are saved. The names of the friends in the array are stored in order of rank, as follows:

>db.b.find ({"Name": "Jim"})

{"_id": ObjectId ("544f3bf8cdb02668db9ab229"), "name": "Jim", "Friends": ["t

Om "," Jack "," Luke "," Rose "," James "," Sam "," Peter "}

Mongodb can find the names of people who are named in the rankings, such as the first person to find Jim 's friends:

> Db.b.find ({"Name": "Jim"},{"Friends": {"$slice": [0,1]}})

{"_id": ObjectId ("544f3bf8cdb02668db9ab229"), "name": "Jim", "Friends": ["t

Om "]}

but can't find the number of "Luke" among Jim's friends. the script for the esproc to solve this problem is:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/5C/23/wKiom1UbUv6R5agLAAFK74-PqFg924.jpg "style=" float: none; "title=" esproc_mongodb_query_index_1.jpg "alt=" Wkiom1ubuv6r5aglaafk74-pqfg924.jpg "/>

A1: Connect mongodb,IP and port number is localhost:27017, database is test, username and password are test. If you need other parameters, you can follow Mongo://ip:port/db?arg=value&. format to continue writing parameters.

A2: Use the Find function to take a number from MongoDB to form a cursor. The collection is b, the filter condition is name=jim, and thespecified key is name and friends. You can see that the find function is similar to the Find function of Mongdb . Esproc cursors are read and processed in batches to avoid excessive amounts of data and memory overflow situations.

A3: Because the amount of data is not big, so here fetch all records of the subject.

A4: Use the POS function to find out where Luke is.

The result of the operation is:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5C/1E/wKioL1UbVDzj73aFAAAZDpfqdnE659.jpg "style=" float: none; "title=" esproc_mongodb_query_index_2.jpg "alt=" Wkiol1ubvdzj73afaaazdpfqdne659.jpg "/>

It is necessary to notethat Esproc does not contain MongoDB 's java driver package. to access MongoDB with Esproc, you must advance the Java driver package for MongoDB (requires 2.12 above version, such as: Mongo-java-driver-2.12.2.jar) is placed in the [esproc installation directory ]\common\jdbc .

EsprocAssistMongodbthe computed script is easily integrated intoJava, just add a rowA5, writtenResult A4you can sendJavaOutputResultSetThe result of the form, the specific code referenceEsprocTutorials. Similarly, withJavacalledEsprocAccessMongodbmust also beMongdbof theJavaThe driver package is placedJavaof the programClasspaththe.




This article is from the High performance report data calculation blog, so be sure to keep this source http://report5.blog.51cto.com/8028595/1627182

The collector computes the array of MongoDB to check the subscript

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.