Sailsjs Mysql ORM在同一个表字段上进行多次查询

时间:2015-04-27 03:53:34

标签: mysql sql node.js sails.js waterline

我使用sails v 0.10.5和最新的sails-mysql

我有一个餐厅过滤系统

Venue.find().populate('comments', {
        deleted: false
    }).where({
            restaurant_services: {contains: '"delivery":1'},
            restaurant_services: {contains: '"takeout":1'},
            restaurant_specialties: {contains: '"breakfast":1'}
    })

现在问题是当我从客户端获取数据时,我不知道用户为restaurat_services选择了多少项,所以显然我必须为.where()函数创建一个动态JSON对象

问题是,我不能这样做

var searchObj = {};
searchObj['restaurant_specialties'] = {contains: '"breakfast":1'}; 

searchObj['restaurant_specialties'] = {contains: '"breakfast":1'};

因此,您可能会看到先前的值设置被第二次替换,

那里的智能人员会非常感谢任何帮助here 但是不起作用

Model.find({
  name: { 'contains' : ['Walter', 'Skyler'] }
});

2 个答案:

答案 0 :(得分:1)

Venue.find().populate('comments', {
    deleted: false
}).where({
        restaurant_services: {
            contains: '"delivery":1',
            contains: '"takeout":1',
        },
        restaurant_specialties: {contains: '"breakfast":1'}
})

你需要

Venue.find({
  where:{
    restaurant_services: {
      contains: '"delivery":1',
      contains: '"takeout":1',
    },
    restaurant_specialties: {
      contains: '"breakfast":"1"'
    }
  }
}).populate('comments', {
    deleted: false
}).exec(console.log);

答案 1 :(得分:1)

为了根据水线的工作原理进行此操作,您需要使用不同的字段进行搜索。

您可以通过在模型中创建别名属性来执行此操作。

venue.js
module.exports.attributes = {
    restaurant_services:'string',
    restaurant_services_1: {type:'string',columnName: 'restaurant_services'}
    restaurant_services_2: {type:'string',columnName: 'restaurant_services'}
    restaurant_services_3: {type:'string',columnName: 'restaurant_services'}
    restaurant_services_4: {type:'string',columnName: 'restaurant_services'}
    restaurant_services_5: {type:'string',columnName: 'restaurant_services'}
}

然后你可以做

Venue.find().populate('comments', {
        deleted: false
    }).where({
            restaurant_services: {contains: '"delivery":1'},
            restaurant_services_1: {contains: '"takeout":1'},
            restaurant_specialties: {contains: '"breakfast":1'}
    })

它的hacky,但它的工作原理