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

How to query array subscript in MongoDB

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to query array subscripts in MongoDB. The content of the article is of high quality. Therefore, Xiaobian shares it with you as a reference. I hope you have a certain understanding of relevant knowledge after reading this article.

Mongodb's set b holds names and friends (arrays). The names in the friends array are stored in ranked order, 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 name of the specified ranking, for example, find the first name among jim's friends:

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

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

om" ] }

However, there is no way to find the ranking value of "luke" among jim's friends. The script for the concentrator esProc to solve this problem is:

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

A2: Use the find function to take numbers from mongodb to form cursors. The set is b, the filter condition is name=jim, and the specified keys are name and friends. You can see that the find function is similar to montdb's find function. esProc cursors read and process data in batches to avoid excessive data volume and memory overflow.

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

A4: Use the pos function to find the location of Luke.

The result of the run is:

Note that esProc does not include the java driver package for mongodb. To access mongodb with esProc, you must put the java driver package of mongodb (version 2.12 or above, such as mongo-java-driver-2.12.2.jar) into [esProc installation directory]\common\jdbc in advance.

esProc help mongodb calculation script is easy to integrate into java, as long as the addition of a line A5, written result A4 can be output to java resultset form of results, specific code reference esProc tutorial. Similarly, calling esProc in java to access mongodb requires putting the java driver package for mongodb into the classpath of the java program.

How to query array subscripts in MongoDB is shared here. I hope the above content can be of some help to everyone and learn more. If you think the article is good, you can share it so that more people can see it.

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: 246

*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

Database

Wechat

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

12
Report