Mongodb.webide: An upgraded version of MongoDB management tool

Source: Internet
Author: User
Tags api manual documentation mongodb driver

Turn from:

Http://www.cnblogs.com/Showshare/p/MongoDB_WebIDE.html

A long-ago collection of articles: "Powerful MongoDB database management tools." Recently just to do some mongodb aspects of work, so turned out to review a bit, it is very convenient to use. However, in the use of the process appeared a number of problems, coupled with more like mongouve of operating habits, so decided to "do their own, clothed", write an upgrade version of the tool.

First, the basic content of the upgrade
    1. The original was developed with the WebForm, and the new version intends to upgrade to MVC
    2. The front-end framework uses bootstrap
    3. MongoDB driver changed to use the official version of the driver
    4. The original query data is the way to parse the SQL statement, the new version is intended to use JavaScript shell command
    5. The original function points have some auxiliary improvements.

The following in the introduction of the various functions of the process, the upgrade of the details of each, the changes in the place will be added underline to mark.

Second, the system main interface

The main system interface is as follows:

The main interface is laid out using the bootstrap grid system: The top is banner navigation, the left side is the tree menu area of the Mongo object, and the right side is the main area of the functional operation. You can reload the main screen by clicking the Refresh icon at the top banner and the upper left. The primary zone enters the server-managed interface by default.

 <div class= "Row-fluid" > <div class= "span3 site-left" > <div class= "Form-inline site-left-top" > <i class= "icon-list" ></i>&nbsp;<span> <a href= "/home/servermanager/" t            arget= "MainFrame" >Servers</a></span>&nbsp;            <a action= "Refresh" ><i class= "Icon-refresh" ></i></a>&nbsp;            <a action= "Expand" ><i class= "Icon-chevron-down" ></i></a>&nbsp; <a action= "collapse" ><i class= "icon-chevron-up" ></i></a> </div> <div id= "M ENU "> <ul id=" tree "class=" Ztree "> </ul> </div> </div> <d IV class= "Span9 pr10" > <iframe id= "mainFrame" name= "MainFrame" height= "100%" width= "100%" frameborder= "0" src = "/home/servermanager/" ></iframe> </div></div> 

In this example, there are two test servers in the tree menu, which are two MongoDB instances built on different ports in this machine, used for the simulation of the master and slave libraries (this is used later).

Third, the tree menu of MONGO objects

First, the tree menu takes the ztree control instead of the original TreeView control. Ztree is a very useful tree control that supports JSON data binding, supports asynchronous loading, is very powerful in controlling and manipulating nodes, and can even edit data directly on a tree node. This control is highly recommended by individuals. Click here to access the Ztree control's website.

Second, the MONGO object uses the hierarchy of the "Server Database" data table field and index. Because both the field and the index are subordinate to the table node, two virtual nodes "table information" and "index" are added to classify the organization. And clicking on a different object node will trigger different actions.

According to the form of mongouve, the table node to the right increases the amount of data in the table display, the field node to the right of the field to increase the data type display. Specific interfaces such as:

The system defines the data structure of the tree node:mongotreenode, which is used to generate the JSON data for the bound tree control. The corresponding enumeration is also defined for each node's object type:mongotreenodetype. The code is as follows:

<summary>//tree node///</summary> public class Mongotreenode {//<summary>        Unique value///</summary> public uint ID {get; set;}        <summary>//parent Node ID///</summary> public uint PID {get; set;}        <summary>/////</summary> public string name {get; set;}    <summary>//////</summary> public mongotreenodetype type {get; set;}        }///<summary>////node type///</summary> public enum Mongotreenodetype {//<summary>        Server///</summary> [Description ("Server")] Server = 1,///<summary> Databases///</summary> [Description ("Database")] Database = 2,///<summary>/ Data Sheet///</summary> [Description ("Data Sheet")] Collection = 3,///<summary>///////</summary> [Description ("field")] field = 4,///<summ        ary>///</summary> [Description ("index")] index =5,///<summary> Table Information Fill node//</summary> [Description ("Table information fills node")] Tablefiller = 6,///<summa ry>///Index fill node///</summary> [Description ("Index fill Node")] Indexfiller = 7}

Ztree controls need to be pre-defined for various properties before binding, please refer to the Ztree control's API manual for details. In this system, the ID and PID fields are pre-bound as a parent-child relationship, and the default value for the control is the ID and PID fields.

View Code

* Note: In the process of actual use, when invoking the tree control's binding method, occasionally the error "Cannot find the bound field id" is reported, causing the tree control to not display. The problem remains to be solved.

The system also defines the data structure of the MONGO object, which consists of five kinds of "server, database, data table, field and index" (see Code). The ID field of the object corresponds to the ID of the tree node and the PID field, that is, the ID of the database object, which corresponds to the ID field of the database node and the PID field of the table node. The structural definition of these objects is much leaner than the original system.

When reading a data table object, some system tables and tables with special meanings (such as index tables, profile tables) are filtered. If you need to release the display, you can go to the program to block. The code is as follows:

View Code

One improvement is that the data type of the object ID field is changed from the original GUID to the UINT type (in order to ensure that the value is greater than 0, the likelihood that the positive and negative values will repeat after the absolute value is avoided) is used to improve the lookup efficiency and reduce the size of the JSON data. The ID field is the only random value that is automatically generated by the program, which is obtained in a parallel calculation (which is then referred to). To ensure the uniqueness of random numbers in a parallel environment, the article "How to generate globally unique IDs in highly concurrent distributed Systems" is referenced. The final code is simple, and has not been tested so far that there have been no duplicate ID values. The code is as follows:

View Code

MONGO objects and tree nodes are read at the same time and are saved to the cache after the first load, with a cache time of 2 hours. When the cache times out and then clicks on the Tree menu node or other actions, the following interface appears in the main area, requiring reloading of the data:

The method of parallel computing mentioned above is called every time the data is loaded from the database, and this is a major improvement. By using parallel computing, it really improves the efficiency of reading data, and the more objects in the database are more obvious. On the parallel computing learning, I recommend "8 days of playing parallel development" this series of articles, very good. The code of the new system keeps the code of cyclic reading MONGO object, interested friends, can modify the program to experience the difference between the parallel and the loop. To ensure thread safety in parallel computing, tree nodes are cached by HashSet ,HashSet itself is thread-safe, while MONGO objects are cached by Hashtable , and when initialized, You need to use the Synchronized method to ensure thread safety.

View CodeIv. Server Management

The server management interface is displayed in the main zone when it is initially loaded. You can also operate server management by clicking on the "Servers" link at the top left. The main area of the Operation interface, the unified use of the bootstrap in the folding plug-in to display, click on a different folding bar, will display the contents of the bar and collapse other content.

Server management function, the essence is to modify the program "Config\servers.config" files, for which to add or remove nodes. The data stored in the file is used as the starting point for parallel computing to read data.

V. Statistics of server information

Clicking on the server node in the tree menu will display the statistics for that server in the main area. At the top of the main area, some basic information about the server object is displayed. In this system, a large number of information displays are presented using the Ztree control, so the Mongotreenode structure is reused, except that it is not necessary to assign a value to the Mongotreenodetype property.

View the server statistics, which actually correspond to the Admincommand command for MongoDB:db.admincommand{"Serverstatus", 1}. The new system is implemented as follows:

View Code

Here is a brief introduction to MongoDB's Bsondocument. For details, please go to the official documentation: C # and. Net Mongo Driver.

Bsondocument is an official driver for various operations of a basic data, it is a set of key/value pairs, there are three ways to generate:

    1. Use the Add or Set method to View Code
    2. Using the Add or set streaming interface method View Code
    3. Class initialization syntax using C # (this method is recommended) View Code

Many subclasses are derived based on bsondocument, including:

    • commanddocument: The parameter document used to run the command, used in the RunCommand method
    • fieldsdocument: The parameter document used to query the returned field, used in the Setfields method
    • groupbydocument: parameter documentation for group (the usefulness of this is not realized for the time being)
    • querydocument: Parameter document for setting query criteria, using in the Find method
    • sortbydocument: The parameter document used to set the collation, used in the SetSortOrder method
    • updatedocument: A parameter document used to modify data, used in the Update method

Other subclasses and their specific use methods need to be realized in the process of use. In general, what type of operation corresponds to what kind of method. In the parameter interface description of the method invocation, there will be a more obvious identity.

Six, the server's master-slave synchronization information

In the first section of the article, it is explained that there is a master-slave environment in this machine, where 127.0.0.1:27017 is the primary server, where 127.0.0.1:28018 is from the server. By clicking on the "View sync Information" link, you can enter the page to view the synced information.

Primary server information:

The synchronization log of the master server, the new system is set to display only 10 records, can be modified by themselves:

From server information:

Source information from the server:

The server's master-slave information is also obtained using the Admincommand command. The synchronization log for the primary server is read from the "Oplog. $main" table in the local library, and the source information from the server is read from the "Sources" table in the local library. Implementation code:

View CodeVii. statistical information of the database

Clicking the database node in the tree menu displays the statistics for that database in the main area. At the top of the main area, some basic information about the database object is displayed.

The statistics for the database are obtained using the RunCommand command, because it is the statistics of a particular database, so you do not need to use the admin library, that is, you do not use Admincommand. The code is as follows:

View CodeViii. profile optimization of the database

On the statistics page of the database, click on the "Profile optimization" link, which is the interface to enter the profile settings of the database. There are three types of profile:

    • do not open : that is, pofile data is not recorded so that efficiency analysis is not possible
    • record Slow command : Only record slow performance of the profile data, you can set your own judgment is the slow command execution time threshold, generally recommended this way
    • record All commands : Records all executed profile data, because it also requires system resources and overhead to record the profile data, so it is not recommended to turn on the record all commands

View profile data to adjust the number of displayed bars yourself. The data type returned by the official drive is Systemprofileinfo, and you can view the source code for detailed information. In this system, several of these attributes have been selected for conversion and presented in a more intuitive list format.

Some tips for optimization:

    1. When the "number of scanned records" is much larger than "return records", consider optimizing the query efficiency by increasing the index
    2. When "return result set" is too large, then the result set we returned is too large, consider returning only the fields you need
    3. If the amount of write queries or update volume is too large, it would be advantageous to add more indexes; When performing the update operation, check the "number of scanned records" and use the index to reduce the number of document scans
    4. If you have few reads, try not to add indexes, because the more indexes you have, the slower the write operation, and the more you need to consider creating an appropriate index if you are doing a lot of reading.
IX. statistical information of data sheets

Clicking the Data Table node in the tree menu displays the statistics for that data table in the main area. At the top of the main area, some basic information about the data table object is displayed.

The statistics for the data table are obtained using the RunCommand command, which requires that the command be executed on a specific database and the name of the corresponding table needs to be passed in. The code is as follows:

View CodeX. Viewing data

You can access the data viewing page by clicking on the "View Data" link on the Data Sheet statistics page, or by clicking the "Table Information" node in the tree menu. Note: The child node of the table information is the field node, which points to this node without any action.

Data query operation mode abandoned the original system parsing SQL statements, using a similar mongouve operation form, input JSON format of the command to query. The new system can set parameters for find, sort, skip, limit. The results of the query are presented in a list, with the field name displayed in the header and the data type of the field. When the data is displayed, the serialized string is processed when the data is of type bsondocument.

When implementing a query using JavaScript shell commands, I almost went around a big circle: At first I felt the need to do a JavaScript shell command parser, and then cooperate with MongoDB.Driver.Builders.Query class to generate the querydocument for the query. At first, the parsing of an or command was done, for example:{$or: [{Word: ' Wenchuan County '},{pos:{$gt:]}, it feels too difficult, how can all kinds of nesting parse out? How can the official drive be so poorly supported for JavaScript shell commands? So began to turn over the source code, suddenly see a method:bsondocument.parse!! then everything was easy. The query is implemented in the following ways:

View Code

The new system adds the ability to view execution plans. the execution efficiency of query statements can be analyzed here to assist in system optimization. Only the "Find, sort" parameter settings are currently supported.

XI. index management

Click on the "View Index" link on the Data Sheet Statistics page, or click the "Index" node in the tree menu to access the index management page. This page provides the ability to display index lists, delete indexes, and add indexes. Note: The child node of the index is the index node, and the node does nothing.

When you add an index, you can use multiple fields to generate a composite index. It is not currently supported when an internal field is used to set an index when a field is a document type. In addition, the meaning of some of the options at the time the index was created is also explained accordingly.

12. PostScript

For this new version of the system described above, there is still a lack of some of the features, such as the addition of data and modification of operations, mapreduce support, and even custom JavaScript shell command execution (similar to executing a custom SQL statement), etc... The lack of function has time to fill up slowly.

In the introduction of the process also interspersed with a part of the code, but not the system's code to complete the introduction, the reason is that the entire code is almost no difficulty, and the code is the best document, and the entire system of code is not large. The development process is mainly to deepen the understanding of some of the concepts of MongoDB, and then with the official driver of the various methods to substantiate.

So, finally, the question comes ... Where's the code? China xx to find XX? Of course not, code download please click:

Mongodb.webide: An upgraded version of MongoDB management tool

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.