I have an email column in mongoDB where I have to check the fields in array, currently I am using following query
我在mongoDB中有一个电子邮件列,我必须检查数组中的字段,目前我正在使用以下查询
emailArray = [
{ '$regex': /test@email.com/i },
{ '$regex': /test2@email.com/i },
]
'$or': [ { email: emailArray }, { name: 'testUser' } ] }
I am getting following error
我收到了以下错误
Cast to string failed for value "[object Object]" at path "email
对于路径“email”中的值“[object Object]”,转换为字符串失败
I am not sure what is wrong with the query :(
我不确定查询有什么问题:(
1 个解决方案
#1
2
You are doing it wrong. emailArray
should be array of regex also you are missing the $in
in the query.
你做错了。 emailArray应该是正则表达式的数组,你也错过了查询中的$ in。
var emailArray = [ /test@email.com/i, /test2@email.com/i ]
db.collection.find({
'$or': [
{ 'email': { '$in': emailArray } },
{ 'name': 'testUser' }
]
})
#1
2
You are doing it wrong. emailArray
should be array of regex also you are missing the $in
in the query.
你做错了。 emailArray应该是正则表达式的数组,你也错过了查询中的$ in。
var emailArray = [ /test@email.com/i, /test2@email.com/i ]
db.collection.find({
'$or': [
{ 'email': { '$in': emailArray } },
{ 'name': 'testUser' }
]
})