Timofey Biryukov
Timofey Biryukov

Reputation: 376

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?

Upvotes: 2

Views: 1794

Answers (3)

Timofey Biryukov
Timofey Biryukov

Reputation: 376

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
})

Upvotes: 0

daronwolff
daronwolff

Reputation: 2064

I had the same question, i think find the solution.

In queries work similarly to mysql 'in queries'. Each element in the array is treated as 'or'.

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

Check this! Waterline Query

Upvotes: 2

Evilsanta
Evilsanta

Reputation: 1072

I have the same issue.... I wish there is such a solution... but I end up using where.

stuff.find({year:{"<":2014}}).where({year:{">":2012}})

Upvotes: 2

Related Questions