1

I'm using this Node API JSON, which returns Customers, their instances, and the instance versions.

Customers.find({
    "include": {
        "relation": "instances",
        "scope": {
            "include": {
                "relation": "versions"
            }
        }
    }
});

I would like to exclude all customers which do not have any related instances, in the result JSON, there is an "instances" entry with empty [ ]. however when I try to use this in a "where" I get a server error... any ideas, or am I going about this the wrong way?

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
dave-o
  • 47
  • 5
  • What is *the instance version*? Is it a model with a relation defined to customer? – superkhau Mar 03 '15 at 17:49
  • Yes, customer model has many instances, instance model has many versions. The above JSON is working, but I want to exclude all customers that do not have instances. – dave-o Mar 03 '15 at 18:16

1 Answers1

1

If you're using MongoDB as your database, then you could add a where property to your filter at the same level as your first include, like:

var filter = {
    where: {
        relationId: {
            exists: false
        }
    },
    include: {...}
};
Customers.find(filter, function ( err, results ) {...});

See issue #1009 for details/updates re: database implementation.

Alternatively, you could just filter the results with Lodash:

var customersWithInstances = _.filter( customers, function ( customer )
{
    return customer.instanceId;
});
Terekhov
  • 141
  • 1
  • 7