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解释执行。
本文作者:Journey&Flower
本文链接:https://www.cnblogs.com/JourneyOfFlower/p/18030359
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步