[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.