相等条件在 groupby mongodb 中无法正常工作
equal condition is not working properly in groupby mongodb
相等条件 ($eq) 在 mongo 数据库中无法正常工作。下面是我的 mongo 数据库文档 (table).
{
"_id":ObjectId("55b08b169d735e293624504a"),
"a":[
{
"acid":139,
"akey":"love",
"atp":"abcd",
"aadd":" ",
"adur":3073
}
],
"created": ISODate("2015-07-23T06:35:02.959 Z")
}
我的查询是
[
{
"$match":{
"created":{
"$gte": ISODate("2015-01-19T07:26:49.045 Z"),
"$lte": ISODate("2015-08-20T07:37:56.045 Z")
}
}
},
{
"$match":{
"cid":{
$nin:[
"59290C6FCCB7E82BD3861F9B6EB46930_2017dec8-0c2c-40c5-9c33-4e3ced0d692f",
"F098F7DBEFCBAE3ED0E815DE694F2307_8fbe1abc-0d11-443d-bd0a-bf5f181673de",
"9BAE0D8CA2A3B4BB641C8CCA2A0BD935_d7a76cf4-eb42-41f8-a851-06dd25269fbf"
]
}
}
},
{
"$unwind":"$a"
},
{
$group:{
_id:"$a.acid",
"PrestoBarImpression":{
"$sum":{
"$cond":[
{
"$eq":[
"$a.atp",
"abcd"
]
},
"$total",
1
]
}
},
"entries":{
$sum:1
}
}
}
]
我得到以下输出。
{
"result":[
{
"_id":139,
"PrestoBarImpression":0,
"entries":1.0000000000000000
}
],
"ok":1.0000000000000000
}
如果我用 ne 而不是 eq,我会得到所需的输出,因为印象数为 1,条目数为 1。
请让我知道我在查询中做错了什么
$eq
operator works by comparing two values and returns true when the values are equivalent, false when the values are not equivalent. Thus in your $cond
operator, when the $eq
operator expression evaluates to true, $cond
should return a value of 1 and if the $eq
expression evaluates to false then it should return 0 so that the accumulator operator $sum
给出了实际总数。
因此,您的 $group
管道阶段应如下所示:
{
$group: {
_id: "$a.acid",
"PrestoBarImpression": {
"$sum": {
"$cond": [
{
"$eq": [ "$a.atp", "abcd" ]
},
1,
0
]
}
},
"entries": { $sum: 1 }
}
}
相等条件 ($eq) 在 mongo 数据库中无法正常工作。下面是我的 mongo 数据库文档 (table).
{
"_id":ObjectId("55b08b169d735e293624504a"),
"a":[
{
"acid":139,
"akey":"love",
"atp":"abcd",
"aadd":" ",
"adur":3073
}
],
"created": ISODate("2015-07-23T06:35:02.959 Z")
}
我的查询是
[
{
"$match":{
"created":{
"$gte": ISODate("2015-01-19T07:26:49.045 Z"),
"$lte": ISODate("2015-08-20T07:37:56.045 Z")
}
}
},
{
"$match":{
"cid":{
$nin:[
"59290C6FCCB7E82BD3861F9B6EB46930_2017dec8-0c2c-40c5-9c33-4e3ced0d692f",
"F098F7DBEFCBAE3ED0E815DE694F2307_8fbe1abc-0d11-443d-bd0a-bf5f181673de",
"9BAE0D8CA2A3B4BB641C8CCA2A0BD935_d7a76cf4-eb42-41f8-a851-06dd25269fbf"
]
}
}
},
{
"$unwind":"$a"
},
{
$group:{
_id:"$a.acid",
"PrestoBarImpression":{
"$sum":{
"$cond":[
{
"$eq":[
"$a.atp",
"abcd"
]
},
"$total",
1
]
}
},
"entries":{
$sum:1
}
}
}
]
我得到以下输出。
{
"result":[
{
"_id":139,
"PrestoBarImpression":0,
"entries":1.0000000000000000
}
],
"ok":1.0000000000000000
}
如果我用 ne 而不是 eq,我会得到所需的输出,因为印象数为 1,条目数为 1。
请让我知道我在查询中做错了什么
$eq
operator works by comparing two values and returns true when the values are equivalent, false when the values are not equivalent. Thus in your $cond
operator, when the $eq
operator expression evaluates to true, $cond
should return a value of 1 and if the $eq
expression evaluates to false then it should return 0 so that the accumulator operator $sum
给出了实际总数。
因此,您的 $group
管道阶段应如下所示:
{
$group: {
_id: "$a.acid",
"PrestoBarImpression": {
"$sum": {
"$cond": [
{
"$eq": [ "$a.atp", "abcd" ]
},
1,
0
]
}
},
"entries": { $sum: 1 }
}
}