【发布时间】:2012-04-29 13:01:00
【问题描述】:
我正在使用 MongoDB 存储有关咖啡的信息,但查询时遇到问题。
我的文档结构是:
{ _id: "Thailand Bok", tags: ["Strong", "Smooth", "Dark"] }
我正在尝试创建允许我搜索名称或标签的查询。
因此,鉴于查询字符串可能是“Thailand Bok”或 [“Strong”、“Smooth”],我想搜索包含 _id 或每个标签的请求。
如果我用 SQL 术语思考,可能是这样的:
"WHERE `_id` like 'Not present%' OR ("Strong" IN `tags` AND "Smooth" IN `tags`)"
到目前为止我的查询是:
{
$or: [
{ _id: { $regex: '^Not present', '$options': 'i' } },
{
$and: [
{ tags: 'Strong' },
{ tags: 'Smooth' }
]
}
]
}
编辑:更正查询中的错误并澄清如果_id匹配或标签匹配,它应该可以工作
【问题讨论】:
-
你觉得怎么样?你需要
$and周围的'吗?
标签: mongodb