MongoDB从输入元素聚合第二个元素?
要从输入元素聚合第二个元素,请使用mapReduce()
。Map-reduce是一种数据处理范例,用于将大量数据压缩为有用的聚合结果。让我们创建一个包含文档的集合-
> db.demo621.insert({ _id: 101, Name1: "John", Name2: "John" });WriteResult({ "nInserted" : 1 })
> db.demo621.insert({ _id: 102, Name1: "Bob", Name2: "John" });
WriteResult({ "nInserted" : 1 })
> db.demo621.insert({ _id: 103, Name1: "Chris", Name2: "John" });
WriteResult({ "nInserted" : 1 })
> db.demo621.insert({ _id: 104, Name1: "Sam", Name2: "John" });
WriteResult({ "nInserted" : 1 })
在find()
方法的帮助下显示集合中的所有文档-
> db.demo621.find();
这将产生以下输出-
{ "_id" : 101, "Name1" : "John", "Name2" : "John" }{ "_id" : 102, "Name1" : "Bob", "Name2" : "John" }
{ "_id" : 103, "Name1" : "Chris", "Name2" : "John" }
{ "_id" : 104, "Name1" : "Sam", "Name2" : "John" }
以下是从输入元素聚合第二个元素的查询-
> db.demo621.mapReduce(... function () {
... track++;
... var actualId= this._id;
... delete this._id;
... if ( track % div == 0 )
... emit(actualId, this );
... },
... function() {},
... {
... "scope": { "track": 0, "div": 2 },
... "out": { "inline": 1 }
... }
... )
这将产生以下输出-
{"results" : [
{
"_id" : 102,
"value" : {
"Name1" : "Bob",
"Name2" : "John"
}
},
{
"_id" : 104,
"value" : {
"Name1" : "Sam",
"Name2" : "John"
}
}
],
"timeMillis" : 48,
"counts" : {
"input" : 4,
"emit" : 2,
"reduce" : 0,
"output" : 2
},
"ok" : 1
}
以上是 MongoDB从输入元素聚合第二个元素? 的全部内容, 来源链接: utcz.com/z/322272.html