数据库中有数据:
{
"_id" : ObjectId("5c3c2ade8e40eb21b5ca69f4"),
"merid" : "201901091526",
"merorderno" : "1547447008",
"usersn" : NumberInt(42838),
"payments" : [
{
"body" : {
"accessmode" : NumberInt(2),
"apkpackagename" : "cn.polaris.demo",
"apksign" : "cbaa3d4f",
"appid" : null,
"appname" : "sdk迁移王秀霞",
"bankcardarea" : NumberInt(14),
"bankcardhash" : "22109",
"bankcardmask" : "6789",
"bankid" : "01030000",
"bankname" : null,
"bindcardnum" : NumberInt(11),
"bindid" : NumberInt(1000382959),
"bosstype" : null,
"cardbankname" : null,
"cardphone" : NumberLong(15800006834),
"cardphonearea" : NumberInt(13),
"cardtype" : NumberInt(0),
"cardusername" : "7A98D832B7F95EA5E3EFB5E4210503E2",
"cardusernamehash" : "w",
"cityno" : null,
"correlationId" : "1390aaee-cecc-4a09-9bf7-ca3dd09027d7",
"createdatetime" : NumberLong(20151027102450),
"devserialnum" : "w",
"devtype" : "12",
"effectivedays" : NumberInt(7),
"failtype" : NumberInt(1),
"firstsuctime" : NumberLong(20190106142328),
"firsttrantime" : NumberLong(20150909164918),
"goodssn" : null,
"idno" : "67B09CB332F1A61342CC8A8C2FA189EA75F07C0725AE1583145E5CA3A162",
"idnohash" : "w",
"idnomask" : "3339",
"idtype" : NumberInt(1),
"imei" : "P0BLC3LCUWWW7G0J",
"imsi" : "D409A0A5545157A",
"lastaccessmode" : NumberInt(2),
"lastrspcode" : NumberInt(0),
"lastsuctime" : NumberLong(20151027105120),
"lasttrantime" : NumberLong(20151027105120),
"lastuserip" : "192.168.31.21",
"merchannelid" : "100011",
"merid" : "201901091526",
"merorderdate" : NumberInt(20151019),
"merorderno" : "1547447008",
"mersn" : NumberInt(1234),
"meruserid" : "w",
"orderno" : NumberLong(100000181493),
"ordertype" : NumberInt(1),
"payphone" : NumberLong(15700006935),
"paystyle" : NumberInt(2),
"paytype" : null,
"productsn" : NumberInt(38),
"producttype" : NumberInt(4),
"regdate" : NumberLong(20150909120624),
"riskcode" : "[999999]",
"risksn" : NumberLong(142326411621),
"rspcode" : NumberInt(140000013),
"rspdesc" : "实时风控触发拦截规则",
"tranamt" : NumberInt(500001),
"trannum" : NumberInt(80),
"transn" : NumberLong(100002393863),
"transucnum" : NumberInt(11),
"trantype" : NumberInt(1),
"udid" : "w",
"userip" : "99.217.95.139",
"userphone" : NumberInt(145),
"usersn" : NumberInt(42838),
"usertype" : NumberInt(1),
"verifycodeflag" : NumberInt(1),
"wifimac" : "c8:95:b4:c9:43:95"
},
"riskUseTime" : NumberLong(108),
"created" : ISODate("2019-01-14T06:23:26.402+0000"),
"ruleTriggeredList" : [
{
"contextId" : null,
"ruleDescription" : "场景:1天失败交易限次",
"ruleName" : "contextRuleGroup_failedTransLimitGroup",
"contextDesc" : null,
"outterAction" : "ALLOW",
"innerAction" : null
},
{
"contextId" : null,
"ruleDescription" : "默认规则组:3分钟内银行卡连续金额递减失败交易数超过则触发",
"ruleName" : "contextRuleGroup_continuousTransLimitGroup",
"contextDesc" : null,
"outterAction" : "ALLOW",
"innerAction" : null
},
{
"contextId" : null,
"ruleDescription" : "默认场景",
"ruleName" : "contextRuleGroup_DefaultContext",
"contextDesc" : null,
"outterAction" : "ALLOW",
"innerAction" : null
},
{
"contextId" : null,
"ruleDescription" : "场景:1小时内在多个商户有交易请求",
"ruleName" : "contextRuleGroup_CrossMerchantRulesGroup",
"contextDesc" : null,
"outterAction" : "ALLOW",
"innerAction" : null
},
{
"contextId" : null,
"ruleDescription" : "场景:近24小时内,同一手机号多银行交易",
"ruleName" : "contextRuleGroup_OperatePhoneBindBankcardLimitGroup",
"contextDesc" : null,
"outterAction" : "ALLOW",
"innerAction" : null
},
{
"contextId" : null,
"ruleDescription" : "北京鑫惠文祥商贸有限公司",
"ruleName" : "contextRuleGroup_beijingxinhuiwenxiangshangmaojiejikakuaijie",
"contextDesc" : null,
"outterAction" : "ALLOW",
"innerAction" : null
},
{
"contextId" : "contextRuleGroup_DefaultContext",
"ruleDescription" : "用户等级1",
"ruleName" : "userLevelPrintRule",
"contextDesc" : "默认场景",
"outterAction" : "ALLOW",
"innerAction" : null
},
{
"contextId" : "contextRuleGroup_beijingxinhuiwenxiangshangmaojiejikakuaijie",
"ruleDescription" : "规则:触发单笔限额,当前交易金额为[5000.01]元,规则配置为[5000]元",
"ruleName" : "SuccessAMTLimitSingle_BLOCK",
"contextDesc" : "北京鑫惠文祥商贸有限公司",
"outterAction" : "BLOCK",
"innerAction" : {
"blackList" : null,
"greyList" : null,
"isWarning" : true
}
},
{
"contextId" : "contextRuleGroup_beijingxinhuiwenxiangshangmaojiejikakuaijie",
"ruleDescription" : "规则:触发24小时银行卡维度的成功限额,已经成功交易为[45000.0]元+当前金额[5000.01]元>限额配置值[50000]元",
"ruleName" : "SuccessAMTLimitLast24Hours_bankcardhash_BLOCK",
"contextDesc" : "北京鑫惠文祥商贸有限公司",
"outterAction" : "BLOCK",
"innerAction" : {
"blackList" : null,
"greyList" : null,
"isWarning" : true
}
}
],
"ruleStackList" : [
{
"ruleStackName" : "failedTransLimitGroup",
"includedRules" : [
{
"ruleName" : "FailedTransLimitLast24Hours_bankcardhash_BLOCK",
"ruleParam" : {
"count" : NumberInt(20)
},
"innerAction" : null,
"outterAction" : "ALLOW"
}
],
"contextId" : "contextRuleGroup_failedTransLimitGroup",
"contextDesc" : "场景:1天失败交易限次"
},
{
"ruleStackName" : "continuousTransLimitGroup",
"includedRules" : [
{
"ruleName" : "Continuous3FailedTransIn3MinutesWithAMTDegressive_bankcardhash_BLOCK",
"ruleParam" : null,
"innerAction" : null,
"outterAction" : "BLOCK"
}
],
"contextId" : "contextRuleGroup_continuousTransLimitGroup",
"contextDesc" : "默认规则组:3分钟内银行卡连续金额递减失败交易数超过则触发"
},
{
"ruleStackName" : "ruleGroup_default",
"includedRules" : [
{
"ruleName" : "userLevelPrintRule",
"ruleParam" : null,
"innerAction" : null,
"outterAction" : null
}
],
"contextId" : "contextRuleGroup_DefaultContext",
"contextDesc" : "默认场景"
},
{
"ruleStackName" : "CrossMerchantRulesGroup",
"includedRules" : [
{
"ruleName" : "CrossMerchant_TranReqMultiMersInAnHour_phone_BLOCK",
"ruleParam" : {
"mersCount" : NumberInt(2)
},
"innerAction" : null,
"outterAction" : "ALLOW"
},
{
"ruleName" : "CrossMerchant_TranReqMultiMersInAnHour_bankcardhash_BLOCK",
"ruleParam" : {
"mersCount" : NumberInt(2)
},
"innerAction" : null,
"outterAction" : "ALLOW"
}
],
"contextId" : "contextRuleGroup_CrossMerchantRulesGroup",
"contextDesc" : "场景:1小时内在多个商户有交易请求"
},
{
"ruleStackName" : "OperatePhoneBindBankcardLimitGroup",
"includedRules" : [
{
"ruleName" : "SuccessOperatePhoneBindBankcardLimitLast24Hours_BLOCK",
"ruleParam" : {
"count" : NumberInt(2)
},
"innerAction" : {
"isWarning" : true
},
"outterAction" : "ALLOW"
},
{
"ruleName" : "AnyStatusOperatePhoneBindBankcardLimitLast24Hours_BLOCK",
"ruleParam" : {
"count" : NumberInt(4)
},
"innerAction" : {
"isWarning" : true
},
"outterAction" : "ALLOW"
}
],
"contextId" : "contextRuleGroup_OperatePhoneBindBankcardLimitGroup",
"contextDesc" : "场景:近24小时内,同一手机号多银行交易"
},
{
"ruleStackName" : "SuccessAMTLimitSingle",
"includedRules" : [
{
"ruleName" : "SuccessAMTLimitSingle_BLOCK",
"ruleParam" : {
"tranamt" : NumberInt(500000)
},
"innerAction" : null,
"outterAction" : "BLOCK"
}
],
"contextId" : "contextRuleGroup_beijingxinhuiwenxiangshangmaojiejikakuaijie",
"contextDesc" : "北京鑫惠文祥商贸有限公司"
},
{
"ruleStackName" : "SuccessLimitLast24Hours",
"includedRules" : [
{
"ruleName" : "SuccessAMTLimitLast24Hours_bankcardhash_BLOCK",
"ruleParam" : {
"tranamt" : NumberInt(5000000)
},
"innerAction" : null,
"outterAction" : null
}
],
"contextId" : "contextRuleGroup_beijingxinhuiwenxiangshangmaojiejikakuaijie",
"contextDesc" : "北京鑫惠文祥商贸有限公司"
}
],
"head" : {
"branchflow" : NumberInt(1),
"counttime" : NumberInt(32),
"endtime" : NumberLong(1445235023016),
"firstsrc" : NumberInt(1001005),
"flowid" : NumberInt(1307),
"from" : NumberInt(1300018),
"instanceid" : "152015101914102213070000",
"msgidstr" : "18761",
"reentrant" : null,
"regreentrant" : null,
"route" : [
[
NumberInt(1),
NumberInt(1)
],
[
NumberInt(1),
NumberInt(2)
],
[
NumberInt(1),
NumberInt(3)
]
],
"servicecode" : NumberInt(1400010),
"starttime" : NumberLong(1445235022984),
"step" : NumberInt(4),
"timeout" : null
}
}
]
}
查询其中的orderno值100000181493的对象
db.risk_collection.find({
'payments.body.orderno': {
$eq: 100000181493
}
})
等同于用聚合函数的结果
db.risk_collection.aggregate([{"$unwind":"$payments"},
{"$match":{"payments.body.orderno":"100000181493"}}])
查"outterAction"有"ALLOW" (都说$elemMatch只返回一条数据,不知道为啥全返回,)
db.risk_collection.find({'payments':{'$elemMatch':{"ruleTriggeredList":{'$elemMatch':{"outterAction":"BLOCK"}}}}});
查"outterAction"有"ALLOW"的数量
db.risk_collection.count({'payments':{'$elemMatch':{"ruleTriggeredList":{'$elemMatch':{"outterAction":"ALLOW"}}}}});
https://blog.csdn.net/shiyaru1314/article/details/68496642
https://blog.csdn.net/xiaoliuliu2050/article/details/79881171
https://www.jianshu.com/p/ac71032a8cdd
下面可以很好的说明$elemMatch (MongoDB 权威指南 第2版)