MongoDB (online) optimization
- 1. Find, FindOne
- 2. A public method of operating the Vip_emp_relation
- 3. Number of query records
- 4. Save, insert
- 5. Summary
1. Find, FindOne
Mongotemplate mongotemplate =mongodbclient.getmongotemplate ();D bcollection cursor= Mongotemplate.getcollection ("Vip_batchsend_message"); Basicdbobject Query=NewBasicdbobject (); Query.put ("Sms_code", Sms_code); Query.put ("OPEN_ID", open_id); Jsonobject message=Newjsonobject ();D bcursor Cursor1=cursor.find (query); Logger.info ("-------------Sms_code:" + sms_code + "----open_id:" + open_id + "--" +cursor1.size ());if(Cursor1.size () > 0) {dbobject dbobject=Cursor1.next (); String Corp_code= Dbobject.get ("Corp_code"). toString (); .....
simple description
- code Intent is to modify if the record exists
- just need to determine if there is a complete purpose
- find returned is dbcursor, It's not right here, we just need to know if it exists.
- Modify suggestions
- fixed conditions Advance pre-assembly
- Find to FindOne
- Judging only one record can be queried , with FindOne can, directly get an object, judge can be based on key to get value for subsequent operations
- suggested modifications
Basicdbobject query = new Basicdbobject (), Query.put ("Sms_code", Sms_code), Query.put ("open_id", open_id); Mongotemplate mongotemplate = Mongodbclient.getmongotemplate (); Jsonobject message = new Jsonobject ();D bcollection cursor = mongotemplate.getcollection ("Vip_batchsend_message");D Bobject dbobject = cursor.findone (query), if (Dbobject.ispartialobject ()) { logger.info ("-------------sms_code:" + Sms_code + "----open_id:" + open_id + "--" + 1); String Corp_code = Dbobject.get ("Corp_code"). toString (); String vip_id = Dbobject.get ("vip_id"). toString (); Data Data_corp_code = new data ("Corp_code", Corp_code, Valuetype.param); Data data_vip_id = new data ("Vip_ids", vip_id, Valuetype.param); ......
2. A public method of operating the Vip_emp_relation
public dbcursor selectrelation (string app_user_name, String open_id) throws SQLException {mongotemplate mongotemplate = Mongodbclient.getmongotemplate (); dbcollection cursor = mongotemplate.getcollection (wxconst.table_vip_emp_relation); Map KeyMap = new HashMap (); Keymap.put ("_id", App_user_name + open_id); Basicdbobject querycondition = new Basicdbobject (); Querycondition.putall (KEYMAP); Dbcursor dbcursor = Cursor.find (querycondition); return dbcursor; }
Brief description
- To see a reference to this method, it is essential to make a judgment use, no need to return a cursor
- If you refer to a method that has a long task and then operates, it waits for a long time not to release the resource
Suggested modifications
Public DBObject selectrelation (string app_user_name, String open_id) throws SQLException { Map keyMap = new HashMap (); keymap.put ("_id", App_user_name + open_id); Basicdbobject querycondition = new Basicdbobject (); Querycondition.putall (KEYMAP); Mongotemplate mongotemplate = Mongodbclient.getmongotemplate (); dbcollection cursor = mongotemplate.getcollection (wxconst.table_vip_emp_relation); DBObject dbcursor = Cursor.findone (querycondition); return dbcursor;}
3. Number of query records
Basicdbobject basicdbobject=new Basicdbobject () basicdbobject.put ("Activity_code", Activity_code); Basicdbobject.put ("open_id", open_id); Basicdbobject.put ("Status", "1"); int Count=cursor.find (basicdbobject). Count (); Basicdbobject.put ("Sign_status", "Y"), int sign_count=cursor.find (basicdbobject). Count (); if (sign_count>0) { result= "You have checked in successfully, do not repeat the registration";} else { if (Count > 0) { basicdbobject query=new basicdbobject (); Query.put ("Activity_code", Activity_code); Query.put ("open_id", open_id); ......
Brief description
- Get the number of records based on criteria
- There is no need to get the document cursor before querying the number of records
Suggested modifications
Basicdbobject basicdbobject=new basicdbobject (); Basicdbobject.put ("Activity_code", Activity_code), Basicdbobject.put ("open_id", open_id); Basicdbobject.put (" Status "," 1 "); Long Count=cursor.count (Basicdbobject); Basicdbobject.put ("Sign_status", "Y"); Long Sign_count=cursor.count (Basicdbobject); if (sign_count>0) { result= "you have checked in successfully, do not repeat the check-in";} else { if (Count > 0) { basicdbobject query=new basicdbobject (); Query.put ("Activity_code", Activity_code); Query.put ("open_id", open_id); ......
4. Save, insert
if (Cursor.find (Basicdbobject). Count () > 0) {basicdbobject basicdbobject1=new basicdbobject (); Basicdbobject1.put ("Sign_status", "Y"); Basicdbobject1.put ("Sign_date", Common.DATETIME_FORMAT.format (new Date ())); Basicdbobject update=new basicdbobject (); Update.put ("$set", BasicDBObject1); Cursor.update (Basicdbobj,update,true,false);} else {Basicdbobject dbobject = new Basicdbobject (); Dbobject.put ("_id", app_id + "_" + Activity_code + "_" + open_id); Dbobject.put ("Corp_code", Corp_code); Dbobject.put ("Sign_status", "Y"); Dbobject.put ("Sign_date", Common.DATETIME_FORMAT.format (new Date ())); Dbobject.put ("app_id", app_id); Dbobject.put ("Activity_code", Activity_code); Dbobject.put ("status", "0"); Dbobject.put ("open_id", open_id); Dbobject.put ("VIP", Vip_array.getjsonobject (0)); Dbobject.put ("Modified_date", Common.DATETIME_FORMAT.format (new Date ())); Dbobject.put ("Created_date", Common.DATETIME_FORMAT.format (new Date ())); Cursor.save(DBObject);} ......
-
Simple description
- Save is based on the _id query and then modify, if you have confirmed that the record does not exist can save the search function directly insert
-
Suggested modifying
if (Cursor.find (Basicdbobject). Count () > 0) {basicdbobject basicdbobject1=new basicdbobject (); Basicdbobject1.put ("Sign_status", "Y"); Basicdbobject1.put ("Sign_date", Common.DATETIME_FORMAT.format (new Date ())); Basicdbobject update=new basicdbobject (); Update.put ("$set", BasicDBObject1); Cursor.update (Basicdbobj,update,true,false);} else {Basicdbobject dbobject = new Basicdbobject (); Dbobject.put ("_id", app_id + "_" + Activity_code + "_" + open_id); Dbobject.put ("Corp_code", Corp_code); Dbobject.put ("Sign_status", "Y"); Dbobject.put ("Sign_date", Common.DATETIME_FORMAT.format (new Date ())); Dbobject.put ("app_id", app_id); Dbobject.put ("Activity_code", Activity_code); Dbobject.put ("status", "0"); Dbobject.put ("open_id", open_id); Dbobject.put ("VIP", Vip_array.getjsonobject (0)); Dbobject.put ("Modified_date", Common.DATETIME_FORMAT.format (new Date ())); Dbobject.put ("Created_date", Common.DATETIME_FORMAT.format (new Date ())); Cursor.inseRT (DBObject);} ......
5. Summary
- These are the problems and suggestions that are currently found and will continue to review
- Operation that involves IO is standard enough to be released as late as possible
- Ali's MongoDB default does not start read/write separation (I have tested confirmed), I will add after the test
- The business implementation process, as far as possible structured data, to describe the splicing error or key does not exist exception
- involves cursor close manually as soon as possible
- Other items can also be referenced, or thrown to
MongoDB (online) optimization