sails.jswaterline

Sails.js WaterlineORM how to use $and/$or operator in query


I have a data stored in DB. Here is an example of one document from DB:

{
    "year": 2012,
    "indicator": 0
}

Other documents contains other years - 2013, 2014 etc. and random "indicator". Now I want to select all document that have data in between 2012 to 2014 range and some indicator. I am working with blueprint sails.js api for passing search queries.

I expect there to be a way to pass two queries and make Waterline select documents that match one (or both) of the queries.

So here is what I have tried (just years for now):

1.

 {
        "year": {">=": 2012}
        "year": {"<=": 2014}
 }

Here I got only docs that have 2012 as a year.

2.

 {
        "year": {">=": 2012, "<=": 2014}
 }

Same as 1.

3.

 "or": [{
    "year": {">=": 2012}
  }, {
    "year": {"<=": 2014}
  }]

Same as 1.

4.

 "and": [{
    "year": {">=": 2012}
  }, {
    "year": {"<=": 2014}
  }]

Does not return any document.

Question is: how to get documents in some range in waterlineORM?


Solution

  • As time has passed and this question was not answered, I'll answer it my self.

    This solution does work on the latest version of sails (1.2.3) and MySQL DB:

    await Record.find({
      where: {
        and: [
          {year: {'>': 2005}},
          {year: {'<': 2008}}
        ]
      },
      limit: 3
    })