stack over
stack over

Reputation: 89

select data from mongodb which has different schema for every column

I have mongodb which receive data from firewall , this firewall sending different data to the mongo depending on the log that it is has , so my mongo collection has different records for example it has records like this

{ "_id" : ObjectId("5df2194d2d81db5e845586d7"), "sys" : "2019-11-28", "time" : ISODate("2019-11-28T16:18:15.238Z"), "time_rcvd" : ISODate("2019-11-28T16:18:15.238Z"), "msg" : "", "syslog_fac" : 23, "syslog_sever" : 6, "syslog_tag" : "16:", "procid" : "16", "pid" : "-", "level" : "INFO", "logtype" : "Traffic LOGS", "time2" : "Thu, 28 Nov 2019 16:18:15 +0000", "18:14KayseriNetcom%%01SECLOG/6/SESSION_TEARDOWN(l):IPVer" : "4", "Protocol" : "udp", "SourceIP" : "10.5.5.30", "Country" : "10.10.0.144", "DestinationIP" : "10.10.0.144", "SourcePort" : "56215", "DestinationPort" : "162", "BeginTime" : "1574957770", "EndTime" : "1574957770", "SendPkts" : "1", "SendBytes" : "142", "RcvPkts" : "0", "RcvBytes" : "0", "SourceVpnID" : "0", "DestinationVpnID" : "0", "SourceZone" : "tunnel", "DestinationZone" : "trust", "PolicyName" : "tunnel_to_trust", "CloseReason" : "aged-out", "ApplicationName" : "SNMP." }

and another records like this

{ "_id" : ObjectId("5df2194d2d81db5e845586d1"), "sys" : "2019-11-28", "time" : ISODate("2019-11-28T16:18:13.126Z"), "time_rcvd" : ISODate("2019-11-28T16:18:13.126Z"), "msg" : "", "syslog_fac" : 23, "syslog_sever" : 6, "syslog_tag" : "16:", "procid" : "16", "pid" : "-", "level" : "INFO", "logtype" : "URL LOGS", "time2" : "Thu, 28 Nov 2019 16:18:13 +0000", "18:12KayseriNetcom%%01SECLOG/6/SESSION_TEARDOWN(l):IPVer" : "4", "Protocol" : "icmp", "Country" : "10.10.0.26", "DestinationIP" : "10.10.0.26", "SourcePort" : "8", "DestinationPort" : "0", "BeginTime" : "1574957867", "EndTime" : "1574957867", "SendPkts" : "1", "SendBytes" : "60", "RcvPkts" : "0", "RcvBytes" : "0", "SourceVpnID" : "0", "DestinationVpnID" : "0", "SourceZone" : "tunnel", "DestinationZone" : "trust", "PolicyName" : "tunnel_to_trust", "CloseReason" : "aged-out." }

so the first record has column ApplicationName but the second one not has that column so when I am trying to select ApplicationName using Laravel by this code

$datas = Logss::select('ApplicationName')->offset(1)->limit( 10)->get()->toArray();

I get this error as there is not ApplicationName in the second row

Undefined index: ApplicationName

so how can I solve this problem

Upvotes: 0

Views: 105

Answers (1)

Manu
Manu

Reputation: 119

If you are using jenssegers/laravel-mongodb module (https://github.com/jenssegers/laravel-mongodb).

Then it has an option to check that matches documents that have the specified field.

You can use something like this below :-

Logss::where('ApplicationName', 'exists', true)->get();

Upvotes: 0

Related Questions