Mixing MongoDB query types (and, or)
mongodb
Solution
$and is only supported in version MongoDB 2.0+ I was using 1.8.2
Problem
I am using MongoDB to store information on coffees and I am having problems with a query. My document structure is: ``` { _id: "Thailand Bok", tags: ["Strong", "Smooth", "Dark"] } ``` I am trying to create query that allows me to search either on the name or tags. So given that a query string may be "Thailand Bok" or ["Strong", "Smooth"] I want to search for requests that either contain the _id or each tag. If I were thinking in SQL terms it may be something like this: ``` "WHERE `_id` like 'Not present%' OR ("Strong" IN `tags` AND "Smooth" IN `tags`)" ``` The query I have so far is: ``` { $or: [ { _id: { $regex: '^Not present', '$options': 'i' } }, { $and: [ { tags: 'Strong' }, { tags: 'Smooth' } ] } ] } ``` edit: correct a mistake in the query and clarified that it should work either if the _id matches OR the tags match