6

你如何做一个像下面这样的查询,我想要伦敦的酒店或名字中有希尔顿的酒店?

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

给出这样的错误错误: { "$err" : "$where compile error" }

两个查询分别工作正常: db.hotels.find({$where : "city = /london/i"}) db.hotels.find({$where : "name = /hilton/i"})

4

2 回答 2

7

Now, that mongodb supports $or condition, you can do this like:

db.hotels.find( { $or: [ { name: /hilton/i }, 
                         { city: /london/i } 
                       ] 
              } );
于 2010-12-18T07:45:25.670 回答
5

尝试这个:

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

注意 据我所知$where,每个文档都进行评估,所以它可能会很慢。如果你有一个单一的属性,我会建议像

db.hotels.find({name: /(hilton|london)/i})
于 2009-12-22T11:42:30.473 回答