Adhesive framework series-Mongodb data service usage practices

Source: Internet
Author: User
Tags mongodb server database sharding

In this article, We will practice how to use the Mongodb data service to store new custom data. As we have said before, using the Mongodb data service is as simple as four steps:

1) define an object

2) define metadata

3) configure the Mongodb Data Service

4) Call the API to insert data

Here, we want to use Mongodb to store the information borrowed from books:

Public enum Status {borrow = 1, return = 2, loss = 3,} [MongodbPersistenceEntity ("Test", DisplayName = "", Name = "Book")] public class Book {[MongodbPersistenceItem (MongodbIndexOption = MongodbIndexOption. ascendingAndUnique, IsPrimaryKey = true)] [MongodbPresentationItem (ShowInTableView = true, DisplayName = "primary key")] public string ID {get; set;} [MongodbPersistenceItem (IsTableName = true)] [MongodbPresentationItem (DisplayName = "department", ShowInTableView = true)] public string DeptName {get; set;} [MongodbPersistenceItem (MongodbIndexOption = MongodbIndexOption. descending, IsTimeColumn = true, ColumnName = "T")] [MongodbPresentationItem (MongodbSortOption = MongodbSortOption. descending, DisplayName = "time", ShowInTableView = true)] public DateTime ServerTime {get; set;} [MongodbPresentationItem (DisplayName = "title", ShowInTableView = true)] public string Name {get; set;} [MongodbPersistenceItem (MongodbIndexOption = MongodbIndexOption. ascending)] [MongodbPresentationItem (DisplayName = "borrower", ShowInTableView = true, MongodbFilterOption = MongodbFilterOption. textBoxFilter)] public string UserName {get; set;} [MongodbPersistenceItem (MongodbIndexOption = MongodbIndexOption. ascending)] [MongodbPresentationItem (DisplayName = "state change", ShowInTableView = true, MongodbFilterOption = MongodbFilterOption. dropDownListFilter)] public Status {get; set;} [MongodbPersistenceItem (MongodbIndexOption = MongodbIndexOption. ascending)] [MongodbPresentationItem (DisplayName = "", ShowInTableView = true, MongodbFilterOption = MongodbFilterOption. checkBoxListFilter)] public string Category {get; set;} [MongodbPresentationItem (DisplayName = "Remarks")] public string Memo {get; set ;}}

Including:

1) primary key ID, which is displayed in the table view as a unique index

2) department name, displayed in table view as the table name

3) Name of the title, displayed in table View

4) time, displayed in table view, in reverse order, and in reverse order index

5) The borrower is displayed in the table view and can search and index in the text box.

6) The status changes are displayed in the table view. This is an enumeration. You can search and index it from the drop-down list.

7) books are classified and displayed in the table view. You can use the check boxes to search and index books.

8) remarks, not displayed in table View

In addition, we store this object in the classification named Test, and the database name is Test _ Book _ year (Database sharding ).

 

After defining the data, you only need to call the API to insert the data:

var book = new Book()                {                    DeptName = DeptName.SelectedValue,                    ID = Guid.NewGuid().ToString(),                    Memo = Memo.Text,                    UserName = UserName.Text,                    ServerTime = DateTime.Now,                    Name = BookName.Text,                    Status = (Status)Enum.Parse(typeof(Status), Status.SelectedValue),                    Category = BookCategory.Text                };                MongodbService.MongodbInsertService.Insert(book);

Front-end:

Department <asp: DropDownList ID = "DeptName" runat = "server"> <asp: ListItem> Tech </asp: ListItem> <asp: ListItem> Sales </asp: listItem> <asp: ListItem> Market </asp: ListItem> </asp: DropDownList> & nbsp; Status <asp: dropDownList ID = "Status" runat = "server"> <asp: ListItem> borrow </asp: ListItem> <asp: ListItem> return </asp: ListItem> <asp: listItem> lost </asp: ListItem> </asp: DropDownList> & nbsp; Category <asp: TextBox ID = "BookCategory" runat = "server"> technical book </asp: textBox> & nbsp; name of the book <asp: TextBox ID = "BookName" runat = "server"> joblos biography </asp: TextBox> & nbsp; Borrower <asp: textBox ID = "UserName" runat = "server"> Zhu xi </asp: TextBox> & nbsp; remarks <asp: textBox ID = "Memo" runat = "server"> is a good book </asp: TextBox>

 

As we have said before, to ensure security, if you want to submit data to the Mongodb data service, you must configure it in the configuration service, register the type, and open the configuration Background:

Go to the Mongodb service configuration (client configuration) in the global configuration, enter the configuration item list, and add one:

Enter the complete name of the type, such as Adhesive. Test. WebApp. Book, and change the complete name of the type to Adhesive. Test. WebApp. Book:

This completes the client configuration. For the server, enter the Mongodb server configuration node, and then enter the service configuration list.

Add an item named Adhesive. Test. WebApp. Book, and modify the full type name to Adhesive. Test. WebApp. Book:

Most of the configuration here is similar to the client configuration. You can see from the configuration "Submit to Database" that this is the server configuration. In addition, the server uses eight threads to submit data, while the client only has one thread.

Now, we can open the front-end to submit data:

Insert some data and view it in the background:

Click to go to the List View:

We have inserted three records. Because we have defined Search rules in the column, we can enable advanced data filtering:

For example, we only check that the status change is a lending record:

Let's add several more records. The borrower's other names are as follows:

To search for records of books lent by Chen Feng:

The result is what we need!

 

Of course, grouping statistics and data volume statistics are also available:

 

Let's look at the data!

 

No need to consider the asynchronous queue for data submission, no need to consider the data index, no need to consider the data sharding database, no need to consider the data presentation background, no need to consider the configuration, you only need to write your own business logic code and submit everything to the Mongodb data service. In fact, the Mongodb data service is mainly used to store internal non-business logic logs, statistics, and status data, because the general data backend can fully meet our needs. In the next article, we will introduce how to configure alarms for new data.

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.