[Spring Data MongoDB] learning notes -- MapReduce, mongodb -- mapreduce

Source: Internet
Author: User
Tags mongodb collection

[Spring Data MongoDB] learning notes -- MapReduce, mongodb -- mapreduce

Mongodb MapReduce mainly includes two methods: map and reduce.

For example, assume that the following three records exist:

{ "_id" : ObjectId("4e5ff893c0277826074ec533"), "x" : [ "a", "b" ] }{ "_id" : ObjectId("4e5ff893c0277826074ec534"), "x" : [ "b", "c" ] }{ "_id" : ObjectId("4e5ff893c0277826074ec535"), "x" : [ "c", "d" ] }

The map method calls the emit method and returns the key-value pair. The key is the value of x [I], for example, a, and the value is 1.

function () {    for (var i = 0; i < this.x.length; i++) {        emit(this.x[i], 1);    }}

The reduce method collects statistics on key-value pairs.

function (key, values) {    var sum = 0;    for (var i = 0; i < values.length; i++)        sum += values[i];    return sum;}

The execution result is as follows:

{ "_id" : "a", "value" : 1 }{ "_id" : "b", "value" : 2 }{ "_id" : "c", "value" : 2 }{ "_id" : "d", "value" : 1 }

Assume that the map and reduce methods are stored in map. js and reduce. js respectively, and the mapreduce results can be obtained through the following code.

MapReduceResults<ValueObject> results = mongoOperations.mapReduce("jmr1", "classpath:map.js", "classpath:reduce.js", ValueObject.class);for (ValueObject valueObject : results) {  System.out.println(valueObject);}

The output is

ValueObject [id=a, value=1.0]ValueObject [id=b, value=2.0]ValueObject [id=c, value=2.0]ValueObject [id=d, value=1.0]

ValueObject code

public class ValueObject {  private String id;  private float value;  public String getId() {    return id;  }  public float getValue() {    return value;  }  public void setValue(float value) {    this.value = value;  }  @Override  public String toString() {    return "ValueObject [id=" + id + ", value=" + value + "]";  }}

You can add a MapReduceOptions to add some map-reduce options. The result is saved to the collection.

MapReduceResults<ValueObject> results = mongoOperations.mapReduce("jmr1", "classpath:map.js", "classpath:reduce.js",                                                                      new MapReduceOptions().outputCollection("jmr1_out"), ValueObject.class);

MapReduceOptions can also be instantiated using the static method options.

MapReduceResults<ValueObject> results = mongoOperations.mapReduce("jmr1", "classpath:map.js", "classpath:reduce.js",                                                                      options().outputCollection("jmr1_out"), ValueObject.class);

You can also add a query to filter statistics. Below we exclude documents containing a and B.

Query query = new Query(where("x").ne(new String[] { "a", "b" }));MapReduceResults<ValueObject> results = mongoOperations.mapReduce(query, "jmr1", "classpath:map.js", "classpath:reduce.js",                                                                      options().outputCollection("jmr1_out"), ValueObject.class);

 


An error occurred while inserting an array object in the spring data mongodb collection.

It seems like it overflows.

Teach Spring Data Mongodb auto-incremental ID implementation

You have to find a place to find the current number of IDs, and then query each insert operation, and add after insert.
 

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.