MongoDB之数据查询(字段判断)

使用“$exists”能够判断某个字段是否存在,若是设置为true表示存在,若是随着为false表示不存在。

范例:查询有parents人员的信息
> db.emp.find({"parents":{"$exists":true}}).pretty();
{
        "_id" : ObjectId("599148bd0184ff511bf02b91"),
        "name" : "林A",
        "sex" : "男",
        "age" : 22,
        "sal" : 8000,
        "loc" : "北京",
        "course" : [
                "语文",
                "数学",
                "英语",
                "音乐",
                "政治"
        ],
        "parents" : [
                {
                        "name" : "林A父亲",
                        "age" : 50,
                        "job" : "农民"
                },
                {
                        "name" : "林A母亲",
                        "age" : 49,
                        "job" : "工人"
                }
        ]
}

范例:查询不具备course人员的信息
> db.emp.find({"course":{"$exists":false}}).pretty();
{
        "_id" : ObjectId("599108423268c8e84253be26"),
        "name" : "赵一",
        "sex" : "男",
        "age" : 30,
        "sal" : 1000,
        "loc" : "北京"
}

能够利用此类查询来进行不须要的数据的过滤。强烈建议数据组成一致。

来自 “ ITPUB博客 ” ,连接:http://blog.itpub.net/28536251/viewspace-2144298/,如需转载,请注明出处,不然将追究法律责任。 spa

转载于:http://blog.itpub.net/28536251/viewspace-2144298/.net