【问题标题】:mongodb select all fields which is not null or empty stringmongodb选择所有非空或空字符串的字段
【发布时间】: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


【解决方案1】:

您可以运行以下查询:

db.collection.aggregate([
    { $match: { _id: ObjectId(...) } },
    {
        $replaceRoot: {
            newRoot: {
                $arrayToObject: {
                    $filter: {
                        input: { $objectToArray: "$$ROOT" },
                        cond: {
                            $and: [
                                { $ne: [ "$$this.v", "" ] },
                                { $ne: [ "$$this.v", 0 ] }
                            ]
                        }
                    }
                }
            }
        }
    }
])

这个想法很简单:从$objectToArray 开始,将所有字段作为一个数组。然后,您可以运行 $filter 以删除所有零和空格并将该数组转换回对象 $arrayToObject 将其提升到根级别 ($replaceRoot)。

Mongo Playground

【讨论】:

  • 感谢您的重播,我在我的数据库中应用了此代码并且工作正常,但是如果我只想获取一条记录(其中 id=23423423432)它在添加匹配条件时不起作用
  • @stackover 修改了我的答案,在$replaceRoot 之前使用$match
猜你喜欢
  • 2014-01-09
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 1970-01-01
  • 2021-08-01
  • 1970-01-01
  • 2023-03-30
  • 2021-11-19
相关资源
最近更新 更多