简体   繁体   中英

Sails.js AND condition

I'm using mongoDb with sails.js and need to make search by address string. For example -"Malibu 98000". row in db look like this {
"state" : "CA", "streetNumber" : "120", "streetName" : "Sh", "city" : "Malibu", "zipCode" : "98000" }

But I'm not able to perform a waterline query using the "AND" condition. I've already tried it in several ways but most of them return empty or simply, never return.

    var propertySearchParams = {
       and : [
          or:[
               {streetNumber: {like: "%98000%"}},
               {zipCode: {like: "%98000%"}}
             ],
          or:[
               {streetName: {like: "%Malibu%"}},
               {city: {like: "%Malibu%"}},
               {state: {like: "%Malibu"}}
             ]
        ]
    }

How can I specify "Malibu" AND "98000"? Thanks!

Guess you want the 'andWhere' modifier. Unfortunately there is no 'and' modifier in waterline for query params, but there is the where() function, which acts like andWhere . So, basically, your query should look something like this:

Model.find()
     .where({
             or:[
               {streetNumber: {like: "%98000%"}},
               {zipCode: {like: "%98000%"}}
             ]
          })
    .where({
              or:[
               {streetName: {like: "%Malibu%"}},
               {city: {like: "%Malibu%"}},
               {state: {like: "%Malibu"}}
             ]
          })
    .exec(...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM