Question

I'm on a Node.js Sails app 0.10-rc4 with Waterline on mongoDB (sails-mongo adapter), and I've a model.where criteria condition that works good:

                or:
                [
                    {
                        start: 
                        {
                            '>': new Date(parseInt(req.param('start'))*1000),
                            '<': new Date(parseInt(req.param('end'))*1000)
                        },
                        end: 
                        {
                            '>': new Date(parseInt(req.param('start'))*1000),
                            '<': new Date(parseInt(req.param('end'))*1000)
                        }
                    }
                ]

... but I wanted to add a exception on it, something in algo that's like that :

      (
          (startq > startparam AND startq < endparam)
           OR
          (endq > startparam AND endq < endparam)
      )
       OR
      (
          (startq < startparam AND endq > endparam)            
      )

... but it seems like we can't use more than one OR ?

                or:
                [
                    {
                        start: 
                        {
                            '>': new Date(parseInt(req.param('start'))*1000),
                            '<': new Date(parseInt(req.param('end'))*1000)
                        },
                        end: 
                        {
                            '>': new Date(parseInt(req.param('start'))*1000),
                            '<': new Date(parseInt(req.param('end'))*1000)
                        },
                        {
                            start: 
                            {
                                '<': new Date(parseInt(req.param('start'))*1000)
                            },
                            end: 
                            {
                                '>': new Date(parseInt(req.param('end'))*1000)
                            }
                        }

                    }
                ]

I've tried many ways (with of without bracket)...

                or:
                [
                    {
                        {
                            or:
                            [
                                {
                                    start: 
                                    {
                                        '>': new Date(parseInt(req.param('start'))*1000),
                                        '<': new Date(parseInt(req.param('end'))*1000)
                                    },
                                    end: 
                                    {
                                        '>': new Date(parseInt(req.param('start'))*1000),
                                        '<': new Date(parseInt(req.param('end'))*1000)
                                    }
                                }
                            ]
                        },
                        {
                                start: 
                                {
                                    '<': new Date(parseInt(req.param('start'))*1000)
                                },
                                end: 
                                {
                                    '>': new Date(parseInt(req.param('end'))*1000)
                                }
                        }
                    }
                ]

... no one worked, most for the time I got:

     SyntaxError: Unexpected token {

The docs don't have examples on that syntax, there is a way to do it ?

EDIT:

Thx to Scott, here is the good syntax:

                or:
                [
                    {
                        start: 
                        {
                            '>': new Date(parseInt(req.param('start'))*1000),
                            '<': new Date(parseInt(req.param('end'))*1000)
                        }
                    },
                    {
                        end: 
                        {
                            '>': new Date(parseInt(req.param('start'))*1000),
                            '<': new Date(parseInt(req.param('end'))*1000)
                        }
                    },
                    {
                        start: 
                        {
                            '<': new Date(parseInt(req.param('start'))*1000)
                        },
                        end: 
                        {
                            '>': new Date(parseInt(req.param('end'))*1000)
                        }
                    }
                ]
Was it helpful?

Solution

Neither of your second two examples are valid JSON, because they include nested objects without keys. That's why you're getting the syntax error.

The or key takes an array of clauses (in the form of valid JSON objects) to "or" together. Try:

            or:
            [
                {
                    start: 
                    {
                        '>': new Date(parseInt(req.param('start'))*1000),
                        '<': new Date(parseInt(req.param('end'))*1000)
                    },
                    end: 
                    {
                        '>': new Date(parseInt(req.param('start'))*1000),
                        '<': new Date(parseInt(req.param('end'))*1000)
                    }
                },
                {
                    start: 
                    {
                        '<': new Date(parseInt(req.param('start'))*1000)
                    },
                    end: 
                    {
                        '>': new Date(parseInt(req.param('end'))*1000)
                    }
                }
            ]

Note that your original (working) example isn't really doing an "OR", since you're only putting one object in the array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top