Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mongoDB中带$or的字段的多个条件_Mongodb - Fatal编程技术网

mongoDB中带$or的字段的多个条件

mongoDB中带$or的字段的多个条件,mongodb,Mongodb,我想从MongoDB集合中检索所有文档,其中users字段是数字或字符串,由所有数字组成(1、2、19或“4”、“19”、…) 我质疑: db.getCollection('collection').find( { users: { $or : [ { $type: [1, 16, 18, 19] }, { $regex: /^[0-9]+$/ } ] } } ) 。。。并获取错误

我想从MongoDB集合中检索所有文档,其中
users
字段是数字或字符串,由所有数字组成(1、2、19或“4”、“19”、…)

我质疑:

db.getCollection('collection').find(
    { 
        users: { $or : [ { $type: [1, 16, 18, 19] },
                         { $regex: /^[0-9]+$/ } ]
               }
    }
)
。。。并获取错误“未知运算符$or”

这项工作:

db.getCollection('collection').find(
    { 
        $or: [ 
                {users:  { $type: [1, 16, 18, 19] } },
                {users:  { $regex: /^[0-9]+$/ }}
        ]
    }
)
为什么第一个变体不起作用

db.getCollection('collection').find( {
    $and : [
        { $or : [ { $type: [1, 16, 18, 19] } ] },
        { $or : [ {users:  { $regex: /^[0-9]+$/ } ] }
    ]
} )
这应该行得通

编辑


$or本身必须包含要查询的字段。第二个查询包含该字段

$or :[{field_name:Match_expression},{field_name:Match_expression}...{field_name:Match_expression}]

这将给出错误“未知顶级运算符:$type”,链接的答案描述了另一个问题。链接答案引用了一些手册,其中指出在某些情况下,
$和
可以省略(“隐式
$和
”)。对于
$或
来说,类似的事情似乎根本没有实现。