How can I search for the records filtering in a field that has an undefined value:
db.records.aggregate({
$match: {
myField: "undefined",
}
})
Filter it by $type:6, (mongodb referece, note that this type marked as 'deprecated'):
db.records.aggregate({
$match: {
myField: {'$type':6},
}
})
undefined it tells you that you can't.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.
null values (there's also a deprecated undefined type). This FAQ entry explains querying for nulls and/or missing fields (the same operators are available for the aggregation framework) . For example :db.test.aggregate({'$match':{ 'myField':{'$type':10} }}) would match documents that have a myField defined but its value is null.
$exists? If possible, add a sample document and a regular query that would return it. Looking at your question,'undefined'is a string, just like'Hello, World!'.