tags:

views:

143

answers:

1

How do you do a query like the one below, where I want hotels in London OR hotels which have hilton in their name?

This query db.hotels.find({$where : "name = /hilton/i || city = /london/i"})

gives such an error error: { "$err" : "$where compile error" }

Both queries separately work ok: db.hotels.find({$where : "city = /london/i"}) db.hotels.find({$where : "name = /hilton/i"})

+4  A: 

Try this:

db.hotels.find({
    $where: "/london/i.test(this.city) || /hilton/i.test(this.hotel)"
})

NOTE As far as I understand $where does a per-document evaluation, so it can be pretty slow. If you'd have a single attribute, I'd suggested smth like

db.hotels.find({name: /(hilton|london)/i})
Alexander Azarov
But I am looking for two different fields :( otherwise the second query would be fine
Piotr Zolnierek
Ah! I've updated the answer.
Alexander Azarov
Thanks Alex :) you are aaaalmost right
Piotr Zolnierek
perfect now :) thanks... and yes you are also right about the performance... it is rather slowdoing two separate queries is faster than one combined
Piotr Zolnierek