在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
}热门推荐
10 给老公祝福语简短2026
11 书写大赛祝福语范文简短
12 结婚请柬祝福语简短搞笑
13 爸妈对宝宝祝福语简短
14 给升迁同事祝福语简短
15 女士四十岁祝福语简短
16 除夕祝福语简短创意牛年
17 岁月有芬芳祝福语简短
18 苦行老师祝福语结婚简短