Let say we have a collection structured like this:
{
_id:1,
tag:["speaker","tom"]
},
{
_id:2,
tag:["subject","tomatoes"]
}
{
_id:3,
tag:["subject","space"]
}
I can query for specific elements, such as :
db.tags.find({tag:["speaker","tom"]})
Now is it possible to perform a query with a regex matching the second element of that array ? something like :
db.tags.find({tag:["speaker",/tom.*/]}) ?
A regex on the field tag (ie : db.tags.find({tag:/sp.*/})) would match the entire array, and I'm looking for a way to make it search only on the second one... before considering to have to restructure that collection.
Update
A workaround is to use the $all operator :
db.tags.find({tag:{$all : ["speaker",/tom.*/]} })
which is equivalent to :
db.tags.find({ $and: [ { tag: "speaker }, { tags: /tom.*/ } ] }
However, that's not the correct way to do, since both element are scanned for both the string and the regex.