Javascript mongodb获得最古老的动物地图/减少
我从未尝试过map/reduce 我怎样才能找到每种动物中最老的 我的数据如下:Javascript mongodb获得最古老的动物地图/减少,javascript,mongodb,node.js,mapreduce,Javascript,Mongodb,Node.js,Mapreduce,我从未尝试过map/reduce 我怎样才能找到每种动物中最老的 我的数据如下: [ { "cateory": "animal", "type": "cat", "age": 4, "id": "a" }, { "cateory": "animal", "type": "bird", "age": 3, "id": "b" }, { "cateory": "animal", "type": "cat", "age": 7 "id": "c" },
[
{
"cateory": "animal",
"type": "cat",
"age": 4,
"id": "a"
},
{
"cateory": "animal",
"type": "bird",
"age": 3,
"id": "b"
},
{
"cateory": "animal",
"type": "cat",
"age": 7
"id": "c"
},
{
"cateory": "animal",
"type": "bird",
"age": 4,
"id": "d"
},
{
"cateory": "animal",
"type": "cat",
"age": 8,
"id": "e"
},
{
"cateory": "company",
"type": "Internet",
"age": 5,
"id": "Facebook"
}
]
map = function() {
emit({type: this.type}, {age: this.age});
}
我正在使用node mongodb native。谢谢 映射函数应该如下所示:
[
{
"cateory": "animal",
"type": "cat",
"age": 4,
"id": "a"
},
{
"cateory": "animal",
"type": "bird",
"age": 3,
"id": "b"
},
{
"cateory": "animal",
"type": "cat",
"age": 7
"id": "c"
},
{
"cateory": "animal",
"type": "bird",
"age": 4,
"id": "d"
},
{
"cateory": "animal",
"type": "cat",
"age": 8,
"id": "e"
},
{
"cateory": "company",
"type": "Internet",
"age": 5,
"id": "Facebook"
}
]
map = function() {
emit({type: this.type}, {age: this.age});
}
以及reduce函数:
reduce = function(key, values) {
maxAge = 0;
values.forEach(function(v) {
if (maxAge < v['age']) {
maxAge = v['age'];
}
});
return {age: maxAge};
}
reduce=函数(键、值){
最大年龄=0;
值。forEach(函数(v){
if(最大年龄
您的映射函数应该如下所示:
[
{
"cateory": "animal",
"type": "cat",
"age": 4,
"id": "a"
},
{
"cateory": "animal",
"type": "bird",
"age": 3,
"id": "b"
},
{
"cateory": "animal",
"type": "cat",
"age": 7
"id": "c"
},
{
"cateory": "animal",
"type": "bird",
"age": 4,
"id": "d"
},
{
"cateory": "animal",
"type": "cat",
"age": 8,
"id": "e"
},
{
"cateory": "company",
"type": "Internet",
"age": 5,
"id": "Facebook"
}
]
map = function() {
emit({type: this.type}, {age: this.age});
}
以及reduce函数:
reduce = function(key, values) {
maxAge = 0;
values.forEach(function(v) {
if (maxAge < v['age']) {
maxAge = v['age'];
}
});
return {age: maxAge};
}
reduce=函数(键、值){
最大年龄=0;
值。forEach(函数(v){
if(最大年龄
非常简单:
collection.find({type:'animal'}).sort({animal:-1}).limit(1);
非常简单:
collection.find({type:'animal'}).sort({animal:-1}).limit(1);
如果您将mongoose与mongodb一起使用,下面是如何实现map/reduce如果您将mongoose与mongodb一起使用,下面是如何实现map/reduce,如果this.category=='animal',则仅在map函数中发出某些内容。此外,map reduce命令接受可用于过滤的查询
参数。如果您在type
上有索引,那么query
参数将使您不必在多个文档上运行map()
。@谢谢,终于在测试中找到了query参数。奇怪的是没有文档。只有当this.category==“animal”时,map函数才会发出一些东西。此外,map reduce命令接受一个query
参数,该参数可用于过滤。如果您在type
上有索引,那么query
参数将使您不必在多个文档上运行map()
。@谢谢,终于在测试中找到了query参数。没有文件记录的人。