mongodb字段永远不会为空?

时间:2015-01-30 17:22:39

标签: javascript mongodb

我在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"] ); } 
}

但是“null”比较永远不会是真的,即使字段为空(或为空),我该如何检查呢?

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

编辑:

作为doc的一个例子:

{
    "_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" : "..."
    }
}

感谢您的帮助!

大卫

1 个答案:

答案 0 :(得分:0)

你可以使用:

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

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