是否有可能将聚合函数的结果作为key:count?
例:
我有以下聚合查询:
db.users.aggregate([
{
$group: {
_id: "$role",
count: {
$sum: 1
}
}
}
])
Run Code Online (Sandbox Code Playgroud)
结果如下:
{ "_id" : "moderator", "count" : 469 }
{ "_id" : "superadmin", "count" : 1 }
{ "_id" : "user", "count" : 2238 }
{ "_id" : "admin", "count" : 11 }
Run Code Online (Sandbox Code Playgroud)
所以这一切都很好,但有没有一种方法(可能使用$project)使结果看起来像这样(即使用role键作为键和count值):
{ "moderator": 469 }
{ "superadmin": 1 }
{ "user": 2238 }
{ "admin": 11 }
Run Code Online (Sandbox Code Playgroud)
我可以通过使用JS对结果进行后处理来做到这一点,但我的目标是直接通过聚合函数来完成.
我遇到了在Servlet中正确设置(持久/跨浏览器会话)cookie的数据以及在Filter中读取它的问题.
Servlet的代码(在登录时运行)是:
String encodedValue = new String(Base64
.encodeBase64(req.getParameter("account").getBytes()));
Cookie cookie = new Cookie("projectAuthenticationCookie", encodedValue );
cookie.setMaxAge(24*60*60);
cookie.setPath("/");
res.addCookie(cookie);
Run Code Online (Sandbox Code Playgroud)
这将在响应中获取cookie,但是当我在我的过滤器中使用以下代码读取它时:
Cookie authenticationCookie = null;
Cookie[] cookies = ((HttpServletRequest) request).getCookies();
for (Cookie cookie : cookies){
if ("projectAuthenticationCookie".equals(cookie.getName())) {
authenticationCookie = cookie;
}
}
Run Code Online (Sandbox Code Playgroud)
我只得到我设置的值,所有其他字段都是null,空或不同.例如,最大年龄总是返回-1,因此cookie永远不会持久.

我尝试使用以下命令设置expires-header:
res.setDateHeader("Expires", System.currentTimeMillis() + 24*60*60*1000);
Run Code Online (Sandbox Code Playgroud)
当我读到没有有效的expires-header时,会话将会超时(如果我错了,请纠正我),但这也无济于事......
我想到的一个问题是我在localhost上运行(尝试设置cookie.setDomain("localhost")但也没有运气).我的web服务器/ serclet容器是Jetty 7,但我不认为这是相关的......
任何提示?