MongoDB 字段永远不会为空

mongodb field is never null?

本文关键字:字段 永远 MongoDB      更新时间:2023-09-26

我在js中有以下内容:

var deletedCursor = db.packets.find(
        { "deleted": "1" }
        ).limit(10).sort({ "packet.datetime": -1 });
deletedCursor.forEach( function( doc ){
    print( "Document ID: " + doc["_id"] );
    if( doc["packet"]["otid"]==null ){ print("OTID is NULL"); }else{ print("OTID doesnt show as null: " + doc["packet"]["otid"] ); }
    if( doc["packet"]["dtid"]==null ){ print("DTID is NULL"); }else{ print("DTID doesnt show as null: " + doc["packet"]["dtid"] ); } 
}

但是这种"空"比较永远不会正确,即使字段为空(或空),我该如何检查?

Document ID: 54cbbbef7f1b0586f29b44b6
OTID doesnt show as null: M2PA042C1AB2
DTID doesnt show as null: 

编辑:

作为文档的示例:

{
    "_id" : ObjectId("54cbc2027f1b0586f2a09688"),
    "deleted" : "1",
    "packet" : {
        "datetime" : ISODate("2015-01-30T17:39:30.896Z"),
        "signallingType" : "M2PA",
        "clgNum" : "91255",
        "cldNum" : "",
        "opc" : "6407",
        "dpc" : "327",
        "transState" : "begin",
        "otid" : "M2PA04276210",
        "dtid" : "",
        "sccpCalling" : "5233",
        "sccpCalled" : "5233",
        "imsi" : "",
        "operation" : "initialDPSMS (60)",
        "camelClgNum" : "",
        "camelCldNum" : "",
        "camelCallRefNum" : "",
        "camelImsi" : "...",
        "camelEvent" : "",
        "camelReleaseCause" : "",
        "pcapFileName" : "/home/monitor/tmp/tmp/gsm_eth2_25053_20150130113917.pcap",
        "message" : "..."
    }
}

感谢您的帮助!

大卫

你可以只使用:

 if(doc["packet"]["dtid"].trim())

它将检查字符串是否为空。