如何匹配 MongoDB 聚合框架中的“未定义"值? [英] How to match by 'undefined' value in MongoDB Aggregation Framework?

查看:13
本文介绍了如何匹配 MongoDB 聚合框架中的“未定义"值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何搜索过滤 in 具有 undefined 值的字段的记录:

How can I search for the records filtering in a field that has an undefined value:

db.records.aggregate({
    $match: {
        myField: "undefined",
    }
})

推荐答案

如果要过滤掉缺少某些字段的文档,请使用 $exists 运算符.

If you want to filter out documents that have some fields missing, use the $exists operator.

这适用于我的机器:

> db.test.drop()
true
> db.test.insert( {'Hello':'World!', 'myField':42})
> db.test.insert( {'Hello again':'World!'})
> db.test.aggregate({'$match':{ 'myField':{'$exists':false} }})
{
        "result" : [
                {
                        "_id" : ObjectId("51b9cd2a6c6a334430ec0c98"),
                        "Hello again" : "World!"
                }
        ],
        "ok" : 1
}

包含 myField 的文档不会显示在结果中.

The document that has myField present does not show in the results.

这篇关于如何匹配 MongoDB 聚合框架中的“未定义"值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆