如何在$ project内部使用过滤器内部数组使用聚合
目前,我正在使用mongoDB存储有关我的表格和响应的数据,当我在集合中使用查找时,返回以下数据:
[
{
"_id": 1,
"forms": [
{
"current_version": 0,
"history": [
{
"content": [{"label": "vrau"}],
"responses": [
{"client_id": 1, "response": [{"field": "vrau1"}]},
{"client_id": 1, "response": [{"field": "vrau1"}]},
{"client_id": 2, "response": [{"field": "vrau2"}]},
{"client_id": 2, "response": [{"field": "vrau2"}]},
],
}
],
}
],
"name": "Testing",
},
{
"_id": 2,
"forms": [
{
"current_version": 1,
"history": [
{
"content": [{"label": "vrau"}],
"responses": [
{"client_id": 1, "response": [{"field": "vrau11"}]},
{"client_id": 1, "response": [{"field": "vrau11"}]},
{"client_id": 2, "response": [{"field": "vrau22"}]},
{"client_id": 2, "response": [{"field": "vrau22"}]},
],
}
],
}
],
"name": "Testing2",
},
]
我要做的是仅返回第一个文档中的响应:_id _id
= 1其中client_id
等于1。因此,以下输出是我想要的。
[{
"_id": 1,
"forms": [
{
"history": [
{
"responses": [
{"client_id": 1, "response": [{"field": "vrau1"}]},
{"client_id": 1, "response": [{"field": "vrau1"}]},
],
}
],
}
]
}]
但是我的查询是返回表单字段空的,这是我要做的:
collection.aggregate([
{
"$match" : {
"_id" : 1,
}
},
{
"$project": {
"forms": {
"$filter": {
"input": "$forms",
"as": "form",
"cond": { "$and":[
{"$eq": [ "$$form.history.responses.client_id", 1 ]}
]
}
}
}
}
}
])
上面的代码出了什么问题?
Currently I'm using mongodb to store data about my forms and responses, when I using a find in my collection the following data is returned:
[
{
"_id": 1,
"forms": [
{
"current_version": 0,
"history": [
{
"content": [{"label": "vrau"}],
"responses": [
{"client_id": 1, "response": [{"field": "vrau1"}]},
{"client_id": 1, "response": [{"field": "vrau1"}]},
{"client_id": 2, "response": [{"field": "vrau2"}]},
{"client_id": 2, "response": [{"field": "vrau2"}]},
],
}
],
}
],
"name": "Testing",
},
{
"_id": 2,
"forms": [
{
"current_version": 1,
"history": [
{
"content": [{"label": "vrau"}],
"responses": [
{"client_id": 1, "response": [{"field": "vrau11"}]},
{"client_id": 1, "response": [{"field": "vrau11"}]},
{"client_id": 2, "response": [{"field": "vrau22"}]},
{"client_id": 2, "response": [{"field": "vrau22"}]},
],
}
],
}
],
"name": "Testing2",
},
]
What I'm trying to do is return only the responses from the first document: _id
= 1 where the client_id
is equal to 1. so, the following output is what I want.
[{
"_id": 1,
"forms": [
{
"history": [
{
"responses": [
{"client_id": 1, "response": [{"field": "vrau1"}]},
{"client_id": 1, "response": [{"field": "vrau1"}]},
],
}
],
}
]
}]
but my query Is returning the forms field empty, this is what I'm trying to do:
collection.aggregate([
{
"$match" : {
"_id" : 1,
}
},
{
"$project": {
"forms": {
"$filter": {
"input": "$forms",
"as": "form",
"cond": { "$and":[
{"$eq": [ "$form.history.responses.client_id", 1 ]}
]
}
}
}
}
}
])
What is wrong with the code above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是使用
$降低
将您的多级数组“弄平”到一个响应中。然后,全部剩下的就是$ filter
您需要的响应:playground示例
One way to do it is to use
$reduce
to "flatten" your multi level arrays into one array of responses. Then, all is left is to$filter
the responses you need:Playground example