Search code examples
sails.jswaterline

Sails.js and Waterline: Finding Where Search Term is in the Array or Nested Object


I'm trying to write a complex find query using Waterline from Sails.js. Here's a simplified example of the object that I'm doing the find on:

object description

Basically, I want to write a query where a provided search string could be equal to the id, be in the myArray attribute, or be the value of myNestedObject.nestedAttribute.

I know that I can do an or like this:

MyModel.find().where({ or: [] });

That array that is provided as the or in the .where() is where the matches should be added, like this:

{ id: { 'contains': searchTerm } }

How do I do a query using Waterline where I can provide a search term (searchTerm) to see if that value is in the myArray object? And how can I check nested attributes? I am using the sails-mongo adapter, but I'd like to not write any Mongo specific queries so that I can have all the benefits of using an ORM in the future (like if we move off Mongo or something).


Solution

  • It’s not possible to search within embeds using standard waterline query syntax, however you can try:

    1. Structuring your data differently.
    2. Using a mongo native query.
    3. Using .stream() and check each record one by one if this query isn't latency-sensitive (e.g. a scheduled job or migration script).