在 MongoDB 聚合期间分隔字符串
为此,请使用 mapReduce()。我们首先使用文档创建一个集合 -
> db.splitString.insertOne({"StudentName":"John Smith"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e0849d925ddae1f53b62206")
}下面是使用 find() 方法从集合中显示所有文档的查询 -
> db.splitString.find().pretty();
这将产生以下输出 -
{
"_id" : ObjectId("5e0849d925ddae1f53b62206"),
"StudentName" : "John Smith"
}下面是分隔字符串的查询 -
> db.splitString.mapReduce(
... function() {
... var StudentLastName = this.StudentName.split(/\s/).reverse()[0].toUpperCase();
...
... emit({ "StudentLastName": StudentLastName, "FirstObjectId": this._id },this);
... },
... function(){},
... { "out": { "inline": 1 } }
... );这将产生以下输出 -
{
"results" : [
{
"_id" : {
"StudentLastName" : "SMITH",
"FirstObjectId" : ObjectId("5e0849d925ddae1f53b62206")
},
"value" : {
"_id" : ObjectId("5e0849d925ddae1f53b62206"),
"StudentName" : "John Smith"
}
}
],
"timeMillis" : 32,
"counts" : {
"input" : 1,
"emit" : 1,
"reduce" : 0,
"output" : 1
},
"ok" : 1
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP