Answer a question

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

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

Answers

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

This works on my machine :

> 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
}

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

Logo

MongoDB社区为您提供最前沿的新闻资讯和知识内容

更多推荐