Mongo regex query for field in list of embedded objects
i'm have a collection with records like开发者_如何学C this
> db.company.findOne({companyId:1})
{
"_id" : ObjectId("4e22ff08eefdd839f60ab95f"),
"lastUpdate" : ISODate("2011-07-11T17:00:00Z"),
"errorCount" : 0,
"house" : 49,
"phones" : [
{
"cityCode" : "3852",
"number" : "461423",
"type" : "phone"
},
{
"cityCode" : "3852",
"number" : "461317",
"type" : "phone"
}
],
"houseAdd" : "",
"rubricsId" : [
NumberLong(184108177),
NumberLong(184108175)
],
"companyId" : NumberLong(1)
}
now i'm try to find all companies with phone number start with 8-800 i'm try to search my query
db.company.find({"phones.number":/8-800.*/}
and get an empty list. But
db.company.find({"phones.number":/8.*/})
return all companies which phone numbers starts with eight. AFAIK '-' isn't a special char for regex in this context? I'm try regex '8\-800' with same result. Where's i'm wrong?
Maybe you have some funny Unicode in your data, just because it looks like a hyphen doesn't mean that it is a hyphen. You could try finding the entry you're looking for with findOne
and then running the data through a hex dumper to see if it really is a plain ASCII hyphen or some Unicode thing that looks like a hyphen. If this is the case, then loosen up your search pattern to something like /^8\W+800/
and tweak it until it finds exactly what you're looking for.
BTW, you're not the first person to come across tricky Unicode:
String is not equal to itself
Another possible solution:
/^\D*8\D*800/
^
make sure that this is at the start of the string\D*
any number of non-decimal characters, matches whatever you got between8
and800
(except numbers).
The first \D*
might not be needed if you know that all those strings start with numbers and not anything else.
精彩评论