[Tutorial] MongoDB from entry-level to advanced (data retrieval and statistics

Source: Internet
Author: User
[Tutorial] MongoDB from entry-level to advanced (data retrieval and statistics

[Tutorial] MongoDB from entry to the advanced (data retrieval and statistics-microblogging data sorting): GitHub: https://github.com/magicdict/MagicMongoDBTool this time to explain the MongoDB data statistics capabilities. As a statistical data, I collected the experiment data from fans on Yang Mi's Weibo. Since it can only be one day at most

[Tutorial] MongoDB from entry-level to advanced (data retrieval and statistics-Weibo data sorting)

:

GitHub: https://github.com/magicdict/MagicMongoDBTool

This section describes MongoDB's data statistics capabilities.

As a statistical data, I collected the experiment data from fans on Yang Mi's Weibo. Since up to 5000 followers can be collected each day, there are only 5000 pieces of data.

At the same time, although these materials come from the User class, they do not involve the hierarchical structure and cannot understand the power and server space of the hierarchical database.

The following code is used to collect data. This code is used to demonstrate how to use Sina Weibo's API correctly. MongoDB directly saves objects to the database like an ORM.

Of course, you can download the Sina Weibo API here,

The API function is a bit tricky. Although the number of followers can be Int32 in the method parameter, if you set a number greater than 200, the program reports an error, the parameter type does not match.

I thought it was really a parameter type problem, but there was no error in compiling .... Finally, we found that there is a 200 limit ..... Now that you have limits, you can write Byte... Potholes ....

BtnGetFollowers_Click (object sender, EventArgs e) 2 {3var Srv = SystemManager. GetCurrentServer (); 4if (Srv! = Null) 5 {); 7var oauth = new NetDimension. weibo. OAuth (txtAppKey. text, txtaskrect. text); 8 bool result = oauth. clientLogin (txtWeiBoUsr. text, txtWeiBoPsw. text); {11var Sina = new NetDimension. weibo. client (oauth); 12var uid = Sina. API. account. getUID (); 14int UserCount; 15int TotalCount; 16 UserCount = 0; 17 TotalCount = 0; 18 19 NetDimension. weibo. entities. user. collection followers; {22 followers = Sina. API. friendships. followers (txtSupperStarID. text, "", 150, UserCount, true); 23if (TotalCount = 0) 24 {25 TotalCount = followers. totalNumber; 26} 27 foreach (var follow in followers. users) 28 {29 col. insert (follow); 30 UserCount ++; 31} 32} while (UserCount <TotalCount); 34} 35} 36 else {); 38} 39}

Collected Data: (although not personal information, mosaic it)

[Check the number of entries: Aggregate Count]

5000 records... You don't need to explain this. The Count of aggregation is the Count statistics.

The Collection object has the Count method, which can be called directly. Of course, Count supports conditional filtering.

If (Query. QueryConditionList. Count = 0 |! IsUseFilter) {MyMessageBox. showEasyMessage (, + SystemManager. getCurrentCollection (). count (). toString ();} else {MongoDB. driver. IMongoQuery mQuery = MongoDBHelper. getQuery (Query. queryConditionList); MyMessageBox. showMessage (, + SystemManager. getCurrentCollection (). count (mQuery ). toString (), mQuery. toString (), true );}

[How many people are playing Weibo: Aggregation Distinct]

Distinct is also a common function. Only one record of the same field is counted. For example, if we want to see how many people play Weibo, we can perform the Distinct operation on the province where the user is located.

A total of 36 provinces are numbered. 100 represents unknown. 31 represents Shanghai, Hong Kong servers, and 11 represents Beijing

BsonArray ResultArray = (BsonArray) SystemManager. GetCurrentCollection (). Distinct (strKey, MongoDBHelper. GetQuery (DistinctConditionList ));

[Check the number of people playing Weibo in each province: Aggregation Group]

If you are interested in playing NoSQL, the database will not be poor. Everyone knows what Group is doing. OK,

For the province Group, check the Count number.

The tool is not complete yet. Currently, only the (built-in) Count Group function is provided. Of course, you can modify Reduce and InitFields on your own to obtain other results.

[MapReduce]

There is too little data. MapReduce is used. The Map function is distributed to different data instances in parallel. The Reduce function combines the results of each Map function for final statistics.

Official materials:

MapReduce will be used as a separate topic in the future. Here we will show it...

[Query: I only want to check the name, city, and Gender]

With so many data fields, my eyes are full of flowers. I just want to see the name and city, as well as the gender (find my sister paper )......

Haha, sister of Shanghai .....

Continue mosaic: 500 people, women, 31 provinces, a total of 137 people .....

Flexible Use of queries, Hong Kong virtual hosts, everything is under control.

Core code:

The FindAs method supports query conditions, field display, sorting, and Skip specifying the number of records and number of Records extracted.

Official materials:

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.