In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you the example analysis of java operation mongoDB query, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Detailed explanation of examples of mongo query in java Operation
Foreword:
MongoDB is a database based on distributed file storage. Written in C++ language. Designed to provide scalable high-performance data storage solutions for WEB applications.
MongoDB is a product between relational database and non-relational database, which is the most functional and most like relational database in non-relational database. The data structure he supports is very loose, which is similar to json's bson format, so it can store more complex data types. The most important feature of Mongo is that the query language it supports is very powerful, and its syntax is somewhat similar to the object-oriented query language. It can almost achieve most of the functions similar to the single table query of relational database, and also supports the establishment of data indexing.
It is very convenient to query in visualization tools, similar to {'key':' value'}. This kind of query is also similar to advanced queries. For example, if the value of a field is greater than 5, we can look up {'key': {$gt: 5}}, thanks to the fact that monggo stores data of object types (BSON of JSON binary data) for collection storage, which is very convenient. Other advanced queries, such as less than $lt, greater than or equal to $gte, less than or equal to $lte, in a certain range $in, not in a certain range $nin, and so on, can be queried in the above way.
Now show some code in java to manipulate mongo queries through java. It includes accurate matching query, advanced query, query in two time ranges, fuzzy query, paging implementation and other queries.
See the code for details.
Try {System.out.println ("= * Test 3 starts * = *"); / / first get the collection of mongo library DBCollection object DBCollection data = (DBCollection) mongoClientService.getColectionByName ("mongo library collection name"); BasicDBObject query3 = new BasicDBObject (); query3.put ("name", "Tom") / find the name Tom / / query two time ranges, package Map queryMap = new HashMap () in map; queryMap.put ("$gt", "1496911821071"); queryMap.put ("$lt", "1496915447871"); query3.put ("timeStamp", new BasicDBObject (queryMap)); / / Fuzzy matching rule query query3.put ("businessRuleName", new BasicDBObject ("$regex", "rule")); DBCursor result3 = data.find (query3) String resultCode = "4"; if ("4" .equals (resultCode)) {BasicDBList cond = new BasicDBList (); cond.add ("0"); cond.add ("1"); cond.add ("2"); cond.add ("3"); query3.put ("resultCode", new BasicDBObject ("$nin", cond)); / query that resultCode is not a System.out.println (result3.count ()) / / count () method to get the number of records queried result3.skip (10) .limit (20); / / paging, skip (): starting from which article, limit (): limit the number of records returned while (result3.hasNext ()) {DBObject dbObject = (DBObject) result3.next (); System.out.println (JSON.serialize (dbObject)); System.out.println ("-") } System.out.println (result3.count ()); / / System.out.println ("= * end of Test 3 * = *");} catch (Exception e) {e.printStackTrace (); System.out.println ("- Test 3 abnormal -");}
The above contains many ways of query examples, the main manipulation is DBCollection, BasicDBObject, DBCursor, BasicDBList these four classes, for reference only.
The above is all the contents of the article "sample Analysis of mongoDB queries operated by java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.