如何搜索具有 undefined
值的字段中的记录,同时进行筛选:
db.records.aggregate({
$match: {
myField: "undefined",
}
})
如何搜索具有 undefined
值的字段中的记录,同时进行筛选:
db.records.aggregate({
$match: {
myField: "undefined",
}
})
按$type:6
进行过滤,(mongodb参考文档,请注意此类型已标记为“已弃用”):
db.records.aggregate({
$match: {
myField: {'$type':6},
}
})
如果您想过滤掉缺少某些字段的文档,请使用$exists
操作符。
这在我的机器上可以运行:
> 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的文档不会显示在结果中。
db.records.aggregate({
$match: {
myField: {$exists: true, $eq: null},
}
})
$exists
的东西吗?如果可能,请添加一个示例文档和一个可以返回它的常规查询。从你的问题来看,'undefined'是一个字符串,就像'Hello, World!'一样。 - ixe013