MongoDB (online) optimization

Source: Internet
Author: User
Tags findone mongodb

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
    • Project examples
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
      1. code Intent is to modify if the record exists
      2. just need to determine if there is a complete purpose
      3. find returned is dbcursor, It's not right here, we just need to know if it exists.
      4. Modify suggestions
        1. fixed conditions Advance pre-assembly
        2. Find to FindOne
        3. 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
    • Project examples
 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

      1. To see a reference to this method, it is essential to make a judgment use, no need to return a cursor
      2. 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
    • Project examples
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

      1. Get the number of records based on criteria
      2. 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
    • Project examples
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

      1. 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
    1. These are the problems and suggestions that are currently found and will continue to review
    2. Operation that involves IO is standard enough to be released as late as possible
    3. Ali's MongoDB default does not start read/write separation (I have tested confirmed), I will add after the test
    4. The business implementation process, as far as possible structured data, to describe the splicing error or key does not exist exception
    5. involves cursor close manually as soon as possible
    6. Other items can also be referenced, or thrown to

MongoDB (online) optimization

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.