【问题标题】:'Or' statement in mongo-db compassmongo-db compass 中的“或”语句
【发布时间】:2021-03-05 21:01:13
【问题描述】:
在 Mongodb 指南针中。我想在 ownerName 和 ownerStreet 属性(我没有文本索引)中搜索单词“LLC”的集合。我开始使用:
{ownerName: {$regex: / LLC/i}}
给出 3043 个结果。
但是使用:
{ownerName: {$regex: RegExp(' LLC')},ownerStreet: {$regex: RegExp(' LLC')}}
只给100
意思是这是一个“和”语句而不是一个“或”
如何在指南针中使用“或”语句过滤器?
【问题讨论】:
标签:
mongodb
mongodb-compass
【解决方案1】:
您可以使用$or 运算符。
db.collection.find({
$or: [
{
ownerName: { $regex: RegExp(' LLC') }
},
{
ownerStreet: { $regex: RegExp(' LLC') }
}
]
})
您甚至可以将其简化为
db.collection.find({
$or: [
{
ownerName: / LLC/
},
{
ownerStreet: / LLC/
}
]
})