且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

流星.js &mongoDB - 查询多个字段

更新时间:2023-11-09 22:24:52

答案就在你的疑问中

Venues.find({
        venueName: venueNameVar,
        'venueAddress.neighbourhood': venueLocationVar
});

如果您没有设置 vars 之一,它将看起来像这样...

If you don't have one of your vars set it will look like this...

{
   venueName: undefined,
   'venueAddress.neighbourhood':'someVal'
}

因此它将匹配任何没有名称且位于某个街区的场所.

So it would match any venue that doesn't have a name and is in some neighborhood.

更好的方法是仅在有搜索值时才设置查询条件...

A better approach would be to only set query criteria if there's a value to search...

var query = {};
if(Session.get('venueNameVar')) {
   query.venueName = Session.get('venueNameVar');
}
if(Session.get('venueLocationVar') {
   query.venueAddress = {
       neighbourhood : Session.get('venueLocationVar');
   }
}
return Venues.find(query);

我认为这对你来说会更好一些!

I think this will work a bit better for you!