我有3张桌子:
accounts:[
{id:111,name:'john'},
{id:222,name:'due'}
]
workoutTypes:[
{id:1,title:'a'},
{id:2,title:'b'}
]
accountrecords:[
{_id:10,workoutTypeId:1,accountId:111},
{_id:11,workoutTypeId:1,accountId:222}
]
我使用此命令按照与当前帐户匹配的锻炼类型对所有帐户记录进行分组:
db.workoutTypes.aggregate([
{
$lookup: {
from: "accountrecords",
localField: "_id",
foreignField: "workoutTypeId",
as: "records"
}
},
{
$match: {
$and: [
{ "records": { $ne: [] } },
{ "records.accountId": { $eq: mongoose.Types.ObjectId(accountId) } },
]
}
},
])
我获取与这两个帐户相关的记录的问题。 看到eq命令为非匹配值返回false值。
有一个选项可以只获取与当前帐户匹配的记录吗?
答案 0 :(得分:1)
首先,localField
个workoutTypes
集合中的id
必须是_id
,而不是$redact
。可能是你身边的一个错字。其次,如果要限制内容,则需要使用$match
运算符。 db.workoutTypes.aggregate([
{
$lookup: {
from: "accountrecords",
localField: "_id",
foreignField: "workoutTypeId",
as: "records"
}
},
{$match:{"records.accountId":{$exists:true}, "records.accountId":{$eq:ObjectId("5a604b18280420c03e8d3a23")}}},
{$redact:{
$cond:{
if:{$or:[{$eq:["$accountId",ObjectId("5a604b18280420c03e8d3a23")]},{$not:"$accountId"}]},
then:"$$DESCEND",
else:"$$PRUNE"
}
}}
])
实际上按预期工作,但它实际上并不检查所有子文档是否满足条件。
<强>更新强>:
尝试此查询:
/* 1 */
{
"_id" : ObjectId("5ac8b2b8fb45830ff77cd33b"),
"updatedAt" : ISODate("2018-04-07T11:59:52.583Z"),
"createdAt" : ISODate("2018-04-07T11:59:52.583Z"),
"title" : "Snatch",
"__v" : 0,
"records" : [
{
"_id" : ObjectId("5acb1b666b08493b30deed92"),
"updatedAt" : ISODate("2018-04-09T07:51:02.294Z"),
"createdAt" : ISODate("2018-04-09T07:51:02.294Z"),
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8b2b8fb45830ff77cd33b"),
"repetition" : 4,
"weight" : 120,
"__v" : 0
},
{
"_id" : ObjectId("5acb1b6f6b08493b30deed93"),
"updatedAt" : ISODate("2018-04-09T07:51:11.878Z"),
"createdAt" : ISODate("2018-04-09T07:51:11.878Z"),
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8b2b8fb45830ff77cd33b"),
"repetition" : 5,
"weight" : 130,
"__v" : 0
},
{
"_id" : ObjectId("5ace23aa1c4d590dc049e94e"),
"updatedAt" : ISODate("2018-04-11T15:03:06.824Z"),
"createdAt" : ISODate("2018-04-11T15:03:06.824Z"),
"repetition" : 124,
"weight" : 32,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8b2b8fb45830ff77cd33b"),
"__v" : 0
},
{
"_id" : ObjectId("5ace23b21c4d590dc049e94f"),
"updatedAt" : ISODate("2018-04-11T15:03:14.293Z"),
"createdAt" : ISODate("2018-04-11T15:03:14.293Z"),
"repetition" : 325,
"weight" : 235,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8b2b8fb45830ff77cd33b"),
"__v" : 0
},
{
"_id" : ObjectId("5acc8ef5345125417cca18e3"),
"updatedAt" : ISODate("2018-04-10T10:16:21.709Z"),
"createdAt" : ISODate("2018-04-10T10:16:21.709Z"),
"repetition" : 123,
"weight" : 456,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8b2b8fb45830ff77cd33b"),
"__v" : 0
}
]
}
/* 2 */
{
"_id" : ObjectId("5acdf56afd5ca008d01b706f"),
"updatedAt" : ISODate("2018-04-11T11:45:46.192Z"),
"createdAt" : ISODate("2018-04-11T11:45:46.192Z"),
"title" : "Jerk",
"__v" : 0,
"records" : [
{
"_id" : ObjectId("5acdf597fd5ca008d01b7070"),
"updatedAt" : ISODate("2018-04-11T11:46:31.674Z"),
"createdAt" : ISODate("2018-04-11T11:46:31.674Z"),
"repetition" : 12,
"weight" : 100,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5acdf56afd5ca008d01b706f"),
"__v" : 0
}
]
}
/* 3 */
{
"_id" : ObjectId("5ace336ed36c780e6ce76d5d"),
"updatedAt" : ISODate("2018-04-11T16:10:22.483Z"),
"createdAt" : ISODate("2018-04-11T16:10:22.483Z"),
"title" : "Weight lifting",
"__v" : 0,
"records" : [
{
"_id" : ObjectId("5ace337ed36c780e6ce76d5e"),
"updatedAt" : ISODate("2018-04-11T16:10:38.708Z"),
"createdAt" : ISODate("2018-04-11T16:10:38.708Z"),
"repetition" : 21,
"weight" : 100,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ace336ed36c780e6ce76d5d"),
"__v" : 0
}
]
}
/* 4 */
{
"_id" : ObjectId("5ac8cf5ef14469169d8627e8"),
"updatedAt" : ISODate("2018-04-07T14:02:06.155Z"),
"createdAt" : ISODate("2018-04-07T14:02:06.155Z"),
"title" : "Double under",
"__v" : 0,
"records" : [
{
"_id" : ObjectId("5acc8f11345125417cca18e4"),
"updatedAt" : ISODate("2018-04-10T10:16:49.069Z"),
"createdAt" : ISODate("2018-04-10T10:16:49.069Z"),
"repetition" : 111111,
"weight" : 222222,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8cf5ef14469169d8627e8"),
"__v" : 0
},
{
"_id" : ObjectId("5ace2d7b1c4d590dc049e950"),
"updatedAt" : ISODate("2018-04-11T15:44:59.722Z"),
"createdAt" : ISODate("2018-04-11T15:44:59.722Z"),
"weight" : 23,
"repetition" : 123555555,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8cf5ef14469169d8627e8"),
"__v" : 0
}
]
}
/* 5 */
{
"_id" : ObjectId("5ac8cfdd79a48216b3e048b0"),
"updatedAt" : ISODate("2018-04-07T14:04:13.670Z"),
"createdAt" : ISODate("2018-04-07T14:04:13.670Z"),
"title" : "Push-ups",
"__v" : 0,
"records" : [
{
"_id" : ObjectId("5accc1f1cd58a407c6562f79"),
"updatedAt" : ISODate("2018-04-10T13:53:53.933Z"),
"createdAt" : ISODate("2018-04-10T13:53:53.933Z"),
"repetition" : 123,
"weight" : 456000,
"accountId" : ObjectId("5a604b18280420c03e8d3a23"),
"workoutTypeId" : ObjectId("5ac8cfdd79a48216b3e048b0"),
"__v" : 0
}
]
}
输出是:
ObjectId("5a604b18280420c03e8d3a23")
现在,您只需使用您的猫鼬代码mongoose.Types.ObjectId(accountId)
替换 11-Apr-2018 17:46:00.353 SEVERE [http-nio-8080-exec-5] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [ProfileServlet] in context with path [] threw exception [Servlet execution threw an exception] with root causejava.lang.ClassNotFoundException: freemarker.template.TemplateException
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1275)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1104)
at servlets.ProfileServlet.doGet(ProfileServlet.java:26)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:407)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:844)
,您就应该感觉良好。