db.collection.mapReduce(
function() {emit(key,value);}, //map 函数
function(key,values) {return reduceFunction}, //reduce 函数
{
out: <collection>,
query: <document>,
sort: <document>,
limit: <number>,
finalize: <function>,
scope: <document>,
jsMode: <boolean>,
verbose: <boolean>,
bypassDocumentValidation: <boolean>
}
)
函数 | 解释 |
---|---|
map | 将数据拆分成键值对,交给reduce函数 |
reduce | 根据键将值做统计运算 |
out | 可选,将结果汇入指定表 |
quey | 可选筛选数据的条件,筛选的数据送入map |
sort | 排序完后,送入map |
limit | 限制送入map的文档数 |
finalize | 可选,修改reduce的结果后进行输出 |
scope | 可选,指定map、reduce、finalize的全局变量 |
jsMode | 可选,默认false。在mapreduce过程中是否将数 据转换成bson格式。 |
verbose | 可选,是否在结果中显示时间,默认false |
bypassDocmentValidation | 可选,是否略过数据校验 |
var tags = ["nosql","mongodb","document","developer","popular"];
var types = ["technology","sociality","travel","novel","literature"];
var books=[];
for(var i=0;i<50;i++){
var typeIdx = Math.floor(Math.random()*types.length);
var tagIdx = Math.floor(Math.random()*tags.length);
var tagIdx2 = Math.floor(Math.random()*tags.length);
var favCount = Math.floor(Math.random()*100);
var username = "xx00"+Math.floor(Math.random()*10);
var age = 20 + Math.floor(Math.random()*15);
var book = {
title: "book-"+i,
type: types[typeIdx],
tag: [tags[tagIdx],tags[tagIdx2]],
favCount: favCount,
author: {name:username,age:age}
};
books.push(book)
}
db.books1.insertMany(books);
db.books1.mapReduce(
function(){emit(this.author.name,this.favCount)},
function(key,values){return Array.sum(values)},
{
query:{type:"travel"},
out: "books_favCount"
}
)
> db.books_favCount.find()
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://wwwxz.blog.csdn.net/article/details/124438763
内容来源于网络,如有侵权,请联系作者删除!