【发布时间】:2019-12-30 13:13:09
【问题描述】:
我有 mongodb 文档,其中包含如下数据:
{
"_id": "ObjectId(\"5e09db41d4ccee7f500f7326\")",
"time": "ISODate(\"2019-12-30T09:10:53Z\")",
"syslog_fac": 23,
"syslog_sever": 6,
"syslog_tag": "",
"procid": "",
"pid": "-",
"status": "Allowed",
"logtype": "Firewall Logs",
"date": "2019-12-30",
"Module": "01UM",
"Desc": "Theuserloggedout.(UserName",
"Severity_Level": "6",
"Vsys": "public",
"SourceIP": "10.10.0.57",
"ParentGroup": "/netgrup.com.tr",
"LogonTime": "2019/12/3014:07:20",
"LogoutTime": "2019/12/3014:10:53",
"ObversePackets": "0",
"ObverseBytes": "0",
"ReversePackets": "0",
"ReverseBytes": "0",
"LogoutReason": "AnIPaddressconflictoccurred",
"VSys": "",
"PolicyName": "",
"Country": "",
"DestinationIP": "",
"SourcePort": "",
"DestinationPort": "",
"SourceZone": "",
"DestinationZone": "",
"User": "",
"Protocol": "",
"ApplicationName": "",
"Profile": "",
"Type": "",
"Category": "",
"SubCategory": "",
"Page": "",
"Host": "",
"Referer": "",
"Item": "",
"Action": "",
"level": "",
"SourceNatIP": "",
"SourceNatPort": "",
"BeginTime": "",
"EndTime": "",
"SendPkts": 0,
"SendBytes": 0,
"RcvPkts": 0,
"RcvBytes": 0,
"SourceVpnID": "",
"DestinationVpnID": "",
"CloseReason": "",
"Task": "",
"Ip": "",
"VpnName": "",
"AuthenticationMethod": "",
"Command": "",
"user-name": "",
"attack-type": "",
"interface": "",
"packet-count": "",
"rate-number": "",
"file-name": "",
"file-type": "",
"direction": "",
"detect-type": "",
"virus-name": "",
"signature-id": "",
"event-number": "",
"target": "",
"role": "",
"user": ""
}
如您所见,有很多字段具有空字符串或 0 ,所以我要做的是仅选择具有除 0 或空字符串之外的值的列。
这是我用来从 laravel 执行查询但返回空值的代码,当我直接在 mongo 中应用该代码时,我得到正确的数据,并准确返回我想要的,但从 laravel 返回空值,
$data = Logss::raw(function($collection)use ($_id) {
return $collection->aggregate([ [ '$match' => ['_id'=>'ObjectId(5e09dc63715d622be622c639)']], [ '$replaceRoot'=> [ 'newRoot'=>[ '$arrayToObject'=>[ '$filter'=> [ 'input'=> [ '$objectToArray'=>'$$ROOT' ], 'cond'=> [ '$and'=> [ [ '$ne'=> [ '$$this.v', "" ] ], [ '$ne'=>[ '$$this.v', 0 ] ] ] ] ] ] ] ] ] ]); });
【问题讨论】:
-
我不确定您是如何找到此文档的(搜索),但您可以在
projection中使用条件语句完成所有这些操作
标签: mongodb mongodb-query aggregation-framework