Command failed with error 2 (BadValue): '$in needs an array' The full response is {"ok": 0.0, "errmsg": "$in needs an array", "code": 2, "codeName": "BadValue"},MongoDB查询分组求和
查询分组求和
db.coin_log.aggregate([
{ $match: {
merchan_code: 111111
,uid: 170861762319106048
,category: { $in: [100,200,300] }
,username: { $regex: "real001", $options: "i" }
,coin: { $gt: 0 }
,created_at: {
$gte: 1708617600000
,$lte: 1708703999999
}
} }
,{ $limit: 100 }
,{ $skip: 0 }
,{ $sort: { created_at: -1 } }
,{
$group: {
_id: null
,totalCoin: { $sum: "$coin" }
}
}
])
以下写法与上面写法查询一致,上面的默认也是用 $and
条件拼接
db.sp_coin_log.aggregate([
{ $match: {
$and:[
{tenant_id: 10000}
,{uid: 1771185762319106048}
,{category: { $in: [100,200,300] }}
,{username: { $regex: "real001", $options: "i" }}
,{coin: { $gt: 0 }}
,{created_at: {
$gte: 1708617600000
,$lte: 1708703999999
}}
]
} }
,{ $limit: 100 }
,{ $skip: 0 }
,{ $sort: { created_at: -1 }}
,{
$group: {
_id:null
,totalCoin: { $sum: "$coin" }
}
}
])
出现错误:
一般是这句出现错误 ,category: { $in: [100,200,300] }
Command failed with error 2 (BadValue): '\(in needs an array' on server localhost:27017. The full response is {"ok": 0.0, "errmsg": "\)in needs an array", "code": 2, "codeName": "BadValue"}
原因及解决方案:
注意:老版的DBEaver可能不能兼容以上查询语句,需要Navicat解释执行。