Reputation: 659
I've asked a very similar question on SO in the past, but I'm struggling with this one.
My previous question was regards one-to-one blueprint/waterline query, this however is one-to-many.
I have a sails.js app using a Mongo DB. I have two models/collections which have a one-to-many relationship.
Take the following for example, 3 media
items, one if which has 2 titles associated with it...
[
// media item 1
{
"titles": [
{
"name": "BOARDWALK1",
"createdAt": "2016-07-12T12:12:37.946Z",
"updatedAt": "2016-07-12T21:34:10.879Z",
"media": "5784debe3ad51e3422887bab",
"id": "5784deb53ad51e3422887ba8"
},
{
"name": "BOARDWALK3",
"createdAt": "2016-07-12T12:12:42.980Z",
"updatedAt": "2016-07-12T20:59:05.054Z",
"media": "5784debe3ad51e3422887bab",
"id": "5784deba3ad51e3422887baa"
}
],
"name": "DEF234.mxf",
"createdAt": "2016-07-12T12:12:46.490Z",
"updatedAt": "2016-07-12T12:40:08.328Z",
"mediaInfo": {
"blah": 123
},
"id": "5784debe3ad51e3422887bab"
},
// media item 2
{
"titles": [],
"name": "ABC123.mxf",
"createdAt": "2016-07-12T12:12:49.448Z",
"updatedAt": "2016-07-12T12:37:55.757Z",
"id": "5784dec13ad51e3422887bac"
},
// media item 3
{
"titles": [],
"name": "GHR87635.mxf",
"createdAt": "2016-07-12T21:05:20.716Z",
"updatedAt": "2016-07-12T21:05:20.716Z",
"id": "57855b90f2d7713e22cdf17f"
}
]
I'd like to query (using blueprint, i.e. REST calls) for all items without titles associated with them.
To find where an association exists, you can do...
POST http://localhost:1337/media/find
{
"where" : {
"title" : {
"!" : null
}
}
}
...but I'm struggling to find a way of querying the opposite, where no association exists, like WHERE media.titles.length === 0
Any help is very much appreciated.
Upvotes: 3
Views: 326
Reputation: 3190
You have the right idea to check if the array is zero length. However, using the POST
operation to do a find operation is bad design. In RESTful Sails.js blueprint routes, POST
should only be used for create actions. GET
should be used for queries.
You could try using the * parameter to construct a query like the following:
GET http://localhost:1337/media?titles.length<1
Since I cannot test your particular API, I am hoping the above works. It may require slight tweaking to get it to work. You may find it helpful to use an app like Postman to test query strings.
From an architectural standpoint, you may want to remove the null values, for reference see the Google JSON styleguide.
If a property is optional or has an empty or null value, consider dropping the property from the JSON, unless there's a strong semantic reason for its existence.
Upvotes: 1