Subscribe to DSC Newsletter

esProc Helps with Computation in MongoDB – Query Indexes in an Array

MongoDB can find out elements of a built-in array according to their indexes, but cannot find the indexes through the values of the elements. For example, the elements of an array are names of people stored according to their rankings. In MongoDB, names can be found according to the rankings (indexes of the array), but the values of rankings cannot be determined through names. esProc can help MongoDB in realizing this operation. The following example will teach you how it works in detail.


b – a collection of MongoDB, stores a name and an array of friends. The names in the array of friends are stored in the order of rankings, as shown below:

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

{ "_id" : ObjectId("544f3bf8cdb02668db9ab229"), "name" : "jim", "friends" : [ "t

om", "jack", "luke", "rose", "james", "sam", "peter" ] }


In MongoDB, you can find names through specified rankings. For example, find out the name of the first ranking among Jim’s friends:

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

{ "_id" : ObjectId("544f3bf8cdb02668db9ab229"), "name" : "jim", "friends" : [ "t

om" ] }


But you cannot find out the ranking of “luke”, one of Jim’s friends. To solve the problem, esProc has its own script: 

A1:Connect to the MongoDB database. The IP and port number is localhost:27017, the database name is test and both the user name and the password are test. If any other parameters are needed, write them in line with the format mongo://ip:port/db?arg=value&…

A2:Fetch data from the MongoDB database using find function to create a cursor. The collection is b. The filtering criterion is name=jim and the specified keys are name and friends. It can be seen that this find function is similar to the find function of MongoDB. By fetching and processing data in batches, the esProc cursor can avoid the memory overflow resulting from big data importing.

A3:Since the data are not big, fetch function will fetch them all at once.

A4:Find out the position of luke using pos function.


After the code is executed, the result is as follows:

Please note that esProc hasn’t the java driver of MongoDB. To access MongoDB with esProc, the latter (a driver of 2.12.2 version or above is required, i.e. mongo-java-driver-2.12.2.jar) should be put into the [esProc installation directory]\common\jdbc beforehand.


The script for computation in MongoDB with the assistance of esProc is easy to integrate with Java program. By adding another line of code – A5, which is result A4, the result in the form of resultset can be output to Java program. For detailed code, please refer to esProc Tutorial. In the same way, to access MongoDB by calling esProc code with Java program also requires putting the java driver of MongoDB into the classpath of Java program.

more information :

Views: 306

Tags: an, array, esProc, in, indexes, mongodb, query


You need to be a member of AnalyticBridge to add comments!

Join AnalyticBridge

On Data Science Central

© 2021   TechTarget, Inc.   Powered by

Badges  |  Report an Issue  |  Privacy Policy  |  Terms of Service