4

我收集了有关应用程序的信息。每个文档都有字段ver保存应用程序的版本。例子:

"1.0.0.2" (they are strings)
"2.0.3.10"
"3.1.5.111"

前 3 位是主要版本,最后一位是构建版本。我徘徊,有什么办法可以按应用程序的主要版本分组?

例子

输入集合:

{_id: ..., ver: "1.0.0.1"}, 
{_id: ..., ver: "1.0.0.2"}, 
{_id: ..., ver: "2.2.3.0"}

聚合结果:

[ 
 {ver:"1.0.0", count:2},
 {ver:"2.2.3", count:1}
]

PS我更喜欢聚合框架的解决方案:)

4

1 回答 1

4

You can do it by using $substr operator. Following code will do what you want.

db.myCollection.aggregate({$group: {_id : {$substr : ["$ver", 0,5]}, count:{"$sum" : 1}}});

Response will be as follows :

[
  {"_id" : "1.0.0", "count" : 2},
  {"_id" : "2.2.3", "count" : 1}
]
于 2013-09-23T14:11:35.670 回答