我正在使用mongodb.I必须在我的弹簧数据mongo db中为$ date使用聚合查询。这是我的用户集合。
{
"_id" : NumberLong(70289),
"_class" : "com.samepinch.domain.user.User",
"age" : 25,
"roles" : [
"ROLE_MODERATOR",
"ROLE_USER"
],
"firstName" : "Abhi",
"lastName" : "Saini",
"email" : "[email protected]",
"createdDate" : ISODate("2015-12-04T12:29:57.604Z"),
"updatedDate" : ISODate("2016-02-10T06:23:13.314Z")
}
这是我的MongoDB查询
db.users.aggregate([{ $project : { month_joined : { $month : "$createdDate" } } } ,{ $group : { _id : {month_joined:"$month_joined"} , number : { $sum : 1 } } },{ $sort : { "_id.month_joined" : 1 } }])
查询给了我希望的结果为:
{ "_id" : { "month_joined" : 1 }, "number" : 19 }
{ "_id" : { "month_joined" : 2 }, "number" : 7 }
{ "_id" : { "month_joined" : 9 }, "number" : 16 }
{ "_id" : { "month_joined" : 10 }, "number" : 12 }
{ "_id" : { "month_joined" : 11 }, "number" : 11 }
{ "_id" : { "month_joined" : 12 }, "number" : 13 }
现在我必须写使用mongotemplate春天的mongodb数据这个查询。我是使用聚合的新手,他们是使用它的简单方法。请帮忙
谢谢。
当我使用项目()。 andExpression()。它要求我和表达式(字符串表达式,对象参数)。根据你的回答,我不知道如何使用它? –
如前所示,您是否尝试使用'.andExpression(“month(createdDate)”)'而不是'.andExpression(“month(date)”)? – chridam
其实.andExpression()是要求两个参数我。e和表达式(字符串表达式,对象参数)。我很困惑传递什么。 –