Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays MongoDB聚合管道过滤两个阵列_Arrays_Mongodb - Fatal编程技术网

Arrays MongoDB聚合管道过滤两个阵列

Arrays MongoDB聚合管道过滤两个阵列,arrays,mongodb,Arrays,Mongodb,你能给我一些建议吗?我有这样一份文件: { "_id" : ObjectId("569620270d3ac01895316edb"), "customerId" : NumberLong("2000900000000000022"), "gender" : "MALE", "birthDate" : ISODate("1976-01-06T23:00:00Z"), "someArray" : [ {

你能给我一些建议吗?我有这样一份文件:

{
    "_id" : ObjectId("569620270d3ac01895316edb"),
    "customerId" : NumberLong("2000900000000000022"),
    "gender" : "MALE",
    "birthDate" : ISODate("1976-01-06T23:00:00Z"),
    "someArray" : [
            {
                    "id" : 5411,
                    "firstDate" : ISODate("2014-08-05T16:17:50Z"),
                    "lastDate" : ISODate("2015-10-31T11:55:51Z"),
                    "sumOfAll" : 5677.35,
                    "minAmount" : 9.75,
                    "maxAmount" : 231.72,
                    "innerArray" : [
                            {
                                    "count" : 4,
                                    "amount" : 449.33
                            },
                            {
                                    "count" : 3,
                                    "amount" : 401.31
                            },
                            {
                                    "count" : 7,
                                    "amount" : 617.8000000000001
                            },
                            {
                                    "count" : 4,
                                    "amount" : 465.28999999999996
                            },
                            {
                                    "count" : 2,
                                    "amount" : 212.95999999999998
                            },
                            {
                                    "count" : 4,
                                    "amount" : 497.53999999999996
                            },
                            {
                                    "count" : 3,
                                    "amount" : 278.23
                            },
                            {
                                    "count" : 3,
                                    "amount" : 383.15999999999997
                            },
                            {
                                    "count" : 6,
                                    "amount" : 459.63
                            },
                            {
                                    "count" : 9,
                                    "amount" : 677.19
                            },
                            {
                                    "count" : 4,
                                    "amount" : 393.85
                            }
                    ]
            },
            {
                    "id" : 5812,
                    "firstDate" : ISODate("2014-09-03T17:16:32Z"),
                    "lastDate" : ISODate("2015-11-04T22:59:59Z"),
                    "sumOfAll" : 275.6,
                    "minAmount" : 15,
                    "maxAmount" : 69,
                    "innerArray" : [
                            {
                                    "count" : 1,
                                    "amount" : 17
                            },
                            {
                                    "count" : 1,
                                    "amount" : 15.4
                            },
                            {
                                    "count" : 1,
                                    "amount" : 69
                            },
                            {
                                    "count" : 1,
                                    "amount" : 53.7
                            },
                            {
                                    "count" : 2,
                                    "amount" : 84
                            }
                    ]
            },
            {
                    "id" : 7399,
                    "firstDate" : ISODate("2015-01-12T22:59:59Z"),
                    "lastDate" : ISODate("2015-03-16T22:59:59Z"),
                    "sumOfAll" : 144.73,
                    "minAmount" : 0.84,
                    "maxAmount" : 24.98,
                    "innerArray" : [
                            {
                                    "count" : 5,
                                    "amount" : 50.379999999999995
                            },
                            {
                                    "count" : 5,
                                    "amount" : 55.45
                            },
                            {
                                    "count" : 10,
                                    "amount" : 38.900000000000006
                            }
                    ]
            },
    ]
}

我想过滤两个内部数组,并对它们进行投影。我正在尝试以下查询:

db.sandbox.aggregate([
{ $match: {
           'gender': {$eq : 'MALE'},
           $or: [
                { $and: [{'someArray.id': {$eq: 5411}}, {'someArray.innerArray.count': 4}, {'someArray.innerArray.amount': {$gte: 2}}]},
                { $and: [{'someArray.id': {$eq: 5812}}, {'someArray.innerArray.count': 5}, {'someArray.innerArray.amount': {$gte: 50}}]},
           ]
          }
},
{ $project: {
    gender: 1,
    customerId: 1,
    someArray: { $filter: {
        input: '$someArray',
        as: 'item',
        cond: {
        $and: [ 
            { $or: [
                {$and: [{$eq: ['$$item.id', 5411]}, {$eq: ['$$item.innerArray.count', 4]}, {$gte: ['$$item.innerArray.amount', 2]}]},
                {$and: [{$eq: ['$$item.id', 5812]}, {$eq: ['$$item.innerArray.count', 5]}, {$gte: ['$$item.innerArray.amount', 50]}]},
            ]},
        ]
      }
    }},
}}
]).pretty()

我收到的结果在someArray中没有数据:

{
    "_id" : ObjectId("569620270d3ac01895316edb"),
    "customerId" : NumberLong("2000900000000000022"),
    "gender" : "MALE",
    "someArray" : [ ]
}

我想收到:

{
    "_id" : ObjectId("569620270d3ac01895316edb"),
    "customerId" : NumberLong("2000900000000000022"),
    "gender" : "MALE",
    "birthDate" : ISODate("1976-01-06T23:00:00Z"),
    "someArray" : [
            {
                    "id" : 5411,
                    "firstDate" : ISODate("2014-08-05T16:17:50Z"),
                    "lastDate" : ISODate("2015-10-31T11:55:51Z"),
                    "sumOfAll" : 5677.35,
                    "minAmount" : 9.75,
                    "maxAmount" : 231.72,
                    "innerArray" : [
                            {
                                    "count" : 4,
                                    "amount" : 449.33
                            },
                            {
                                    "count" : 4,
                                    "amount" : 465.28999999999996
                            },
                            {
                                    "count" : 4,
                                    "amount" : 497.53999999999996
                            },
                            {
                                    "count" : 4,
                                    "amount" : 393.85
                            }
                    ]
            }
    ]
}

若我将$eq更改为$gte,我将收到接收结果,但我也想投影innerArray。我如何实现这一点?我应该使用自己的MapReduce作业,还是可以使用聚合管道来完成

MongoDB版本3.2。我还观察到,当我尝试对数组使用多个谓词并仅投影一个元素时,例如:

db.sandbox.find(  {$and: [{'someArray.id': 7399}, {'someArray.sumOfAll': {$gte: 5000}}]}, {'customerId': 1, 'someArray.$': 1}).pretty()
但它让我回想起:

{
    "_id" : ObjectId("569620270d3ac01895316edb"),
    "customerId" : NumberLong("2000900000000000022"),
    "someArray" : [
            {
                    "id" : 5411,
                    "firstDate" : ISODate("2014-08-05T16:17:50Z"),
                    "lastDate" : ISODate("2015-10-31T11:55:51Z"),
                    "sumOfAll" : 5677.35,
                    "minAmount" : 9.75,
                    "maxAmount" : 231.72,
                    "innerArray" : [
                            {
                                    "count" : 4,
                                    "amount" : 449.33
                            },
                            {
                                    "count" : 3,
                                    "amount" : 401.31
                            },
                            {
                                    "count" : 7,
                                    "amount" : 617.8000000000001
                            },
                            {
                                    "count" : 4,
                                    "amount" : 465.28999999999996
                            },
                            {
                                    "count" : 2,
                                    "amount" : 212.95999999999998
                            },
                            {
                                    "count" : 4,
                                    "amount" : 497.53999999999996
                            },
                            {
                                    "count" : 3,
                                    "amount" : 278.23
                            },
                            {
                                    "count" : 3,
                                    "amount" : 383.15999999999997
                            },
                            {
                                    "count" : 6,
                                    "amount" : 459.63
                            },
                            {
                                    "count" : 9,
                                    "amount" : 677.19
                            },
                            {
                                    "count" : 4,
                                    "amount" : 393.85
                            }
                    ]
            }
    ]
}


这不符合我的观点。我什么都不期望。

首先,您在
$match
中使用条件的方式不会产生您想要的结果

{ $and: [{'someArray.id': {$eq: 5411}}, {'someArray.innerArray.count': 4}, {'someArray.innerArray.amount': {$gte: 2}}]}
上面的行将分别验证每个条件,而不是检查每个
innerArray
元素的
count
amount
条件。如果这是你想要的,你应该调查一下接线员

第二,我不相信你能在二级数组上像那样使用
$filter
。您应该首先展开
someArray

db.sandbox.aggregate(
{
$match:
{
性别:{$eq:'男性'},
“someArray.id”:
{
$in:[54115812]
}
}
},
{
$unwind:“$someArray”,
},
{
$项目:
{
性别:1,
客户ID:1,
someArray:
{
id:1,
第一天:1,
最后日期:1,
sumOfAll:1,
米纳蒙特:1,
最大金额:1,
内部阵列:
{
$filter:
{
输入:“$someArray.innerArray”,
作为:“项目”,
条件:
{
$or:
[
{
美元及:
[
{$eq:['$$item.count',4]},
{$gte:['$$item.amount',2]}
]
},
{
美元及:
[
{$eq:['$$item.count',5]},
{$gte:['$$item.amount',50]}
]
}
]
}
}
}
},
}
})

如果需要,您还可以
someArray
元素返回。

首先,您在
$match
中使用条件的方式不会产生您想要的结果

{ $and: [{'someArray.id': {$eq: 5411}}, {'someArray.innerArray.count': 4}, {'someArray.innerArray.amount': {$gte: 2}}]}
上面的行将分别验证每个条件,而不是检查每个
innerArray
元素的
count
amount
条件。如果这是你想要的,你应该调查一下接线员

第二,我不相信你能在二级数组上像那样使用
$filter
。您应该首先展开
someArray

db.sandbox.aggregate(
{
$match:
{
性别:{$eq:'男性'},
“someArray.id”:
{
$in:[54115812]
}
}
},
{
$unwind:“$someArray”,
},
{
$项目:
{
性别:1,
客户ID:1,
someArray:
{
id:1,
第一天:1,
最后日期:1,
sumOfAll:1,
米纳蒙特:1,
最大金额:1,
内部阵列:
{
$filter:
{
输入:“$someArray.innerArray”,
作为:“项目”,
条件:
{
$or:
[
{
美元及:
[
{$eq:['$$item.count',4]},
{$gte:['$$item.amount',2]}
]
},
{
美元及:
[
{$eq:['$$item.count',5]},
{$gte:['$$item.amount',50]}
]
}
]
}
}
}
},
}
})

如果你想的话,你也可以把
someArray
元素放回去。

你能用书面形式而不是用查询的形式描述一下你想要实现什么吗?我想第二部分正好说明了我想要做的事情。为什么你希望在第二次查询的结果中有一个
someArray
元素带有
“sumOfAll”:144.73
?您在查询中指定此字段应大于或等于5000。好的,您给出的查询太多,无法在单个问题中使用。你想让我们帮你哪一个?还是哪个问题?很抱歉。一般的问题是-我想根据一些谓词同时过滤这两个数组,我想根据这些谓词投影结果。你能描述一下你想要实现什么吗,以书面形式而不是作为查询?我认为第二部分正好显示了我正在尝试做的事情。为什么您希望在第二次查询的结果中有一个
someArray
元素带有
“sumOfAll”:144.73
?您在查询中指定此字段应大于或等于5000。好的,您给出的查询太多,无法在单个问题中使用。你想让我们帮你哪一个?还是哪个问题?很抱歉。一般的问题是-我想基于som同时过滤这两个数组
{ $and: [{'someArray.id': {$eq: 5411}}, {'someArray.innerArray.count': 4}, {'someArray.innerArray.amount': {$gte: 2}}]}