Mongo aggregation & Query)

Source: Internet
Author: User
Tags emit


Mongo Official Website: http://www.mongodb.org/

Mongo is used in work, but Mongo has not been systematically studied. It only records some knowledge points in the use of Mongo during work and can be supplemented at any time, to achieve the purpose of summary and memo.

This article mainly Ends Record aggregation and query.


Aggregation)


Count


DB. view_view.count ()

DB. view_view.count ({_ ID: "521842 "})

DB. view_view.find (). Count ()

DB. view_view.find ({_ ID: "521842"}). Count ()


Distinct


DB. view_view.distinct ("_ id ")

DB. view_view.distinct ("View ") View is an array, and the elements of each array are distinct.


Group


 
DB. invoke_stat.group ({key: {IP: true}, Cond: {Date: {$ GTE: isodate ("16:00:00"), $ LT: isodate ("16:00:00") }}, reduce: function (curr, result) {result. count + = curr. times ;}, initial: {count: 0}, finalize: function (result) {result. count = '$' + result. count}); dB. view_view.group ({keyf: function (DOC) {return {view_num: Doc. view. length }}, reduce: function (curr, result) {result. count + = 1; result. id = curr. _ id ;}, initial: {count: 0 }});

The two groups are described as follows:

Key/keyf: The columns to be grouped. The key selects the columns in the table directly. Kef is a function that processes the columns. The function returns an object, for example, {view_num: Doc. view. length}, Doc. view. length is the length of the view in the array column of the table.

Cond: the query condition to be filtered

Reduce: processing functions

Initial: returns the initial value of a column.

Finalize: further processing of reduce results, such as formatting


Mapreduce


 
DB. invoke_stat.mapreduce (function () {var key = This. IP; emit (Key, {r_times: This. times})}, function (Key, emits) {Total = 0; For (var k in emits) {total + = emits [K]. r_times;} return {r_times: Total }}, {out: 'Mr '})

Above: r_times is the name of the column we define to return, IP and times are the columns in the table, Mr is the set where we want to save the mapreduce computing result to mr.

Mapreduce is prototype: function (MAP, reduce, optionsoroutstring). The following describes the three parameters of the function:

Map function, which traverses every document in the set. This indicates the document. It uses the emit method to group the document by pressing the buttons and returns the data to be counted;

Reduce function, which collects and counts data. The two parameters are the key value and data array returned by the map function;

The optionsoroutstring parameter is an object and defines some additional work. For example, the out parameter in the column above puts the statistical result into the Mr set. If the set does not exist, it is created and overwritten if it exists.


The optionsoroutstring object has other keys besides the out key:

The finalize function is the same as the finalize completor of the group. It can process the reduce result;

Query document, filtering documents before map functions;

The sort document sorts Documents before the map function. You must index the sorted fields first;

Limit integer. Set the number of documents before the map function;

Scope document, variables used in JS functions, the client can pass some values through scope;

The Boolean jsmode specifies whether the objects transmitted between map and reduce functions use the bson format or JavaScript Object. The default value is false, indicating that the bson format is used. The advantage is that the intermediate bson data is stored on the hard disk, therefore, the transmitted data volume can be large, but it will affect the performance. using JavaScript objects, the performance is high, but only 0.5 million different key values can be transferred;

Verbos Boolean, true by default, displays detailed time statistics.


The above shows that mapreduce is powerful and can easily implement different statistical functions.


Query)


Mongo query syntax:

DB. access_logs_140701.find ({jxtime: {$ GT: 1407011300, $ LT: 1407011400}, "curl. SKU ":" 99978033 "}). sort ({jxtime:-1 }). skip (1 ). limit (1, 100)

DB. invoke_stat.find ({Date: {$ GTE: isodate ("10:00:00"), $ LT: isodate ("11:00:00 ")}})

DB. view_view.find ({view :{$ size: 10 }}) If the query array length is less than 10, the current Mongo does not directly support Data Length range query. For example, if the query data length is less than 10, it can only be programmed by mapreduce.

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.