Querying models / Contains filter?

1,255 views
Skip to first unread message

Jan Oberhauser

unread,
Oct 22, 2014, 8:44:11 PM10/22/14
to loopb...@googlegroups.com
Hello,

I was wondering how I can search for a specific value in an array. If I have for example the property numbers in a model like this:
numbers: [1,2,3,4]

and now I want to find all models which have have the number "2" in "numbers". So I tried this: 
"where: { number: {inq:2}} }"
but then realized that it is ment to be the other way around. Is there something like a contains filter that I did overlook?

Thanks!

Jan

Simon Ho

unread,
Oct 23, 2014, 6:33:22 PM10/23/14
to loopb...@googlegroups.com
Have you taken a look at this example: https://github.com/strongloop/loopback-example-relations-basic? There are some basics on filtering and querying data. Otherwise, there is more info in the docs here: http://docs.strongloop.com/display/LB/Where+filter.

Jan Oberhauser

unread,
Oct 23, 2014, 10:42:00 PM10/23/14
to loopb...@googlegroups.com
Hello,

thanks but yes. I already checked out everything I could find but found nothing that helps me.

Maybe I should rephrase my question: "Is there an inverted "inq" operator?

With the existing operator(inq) the search-value is an array and the value in the database a single value (for example an "int"). What I want is the exact opposite. I want to give a single value (like an "int") as search-value and I want to look for it in an array-property on the objects.

Thanks!

Raymond Feng

unread,
Oct 23, 2014, 11:39:35 PM10/23/14
to Jan Oberhauser, loopb...@googlegroups.com
The syntax should be:

{where: {number:{inq: [2]}}}

Sent from my iPhone 6 Plus
--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jan Oberhauser

unread,
Oct 23, 2014, 11:55:48 PM10/23/14
to loopb...@googlegroups.com, jan.obe...@gmail.com
Thanks a lot but that is exactly the oppsite of what I want.

To explain it with an example.

If I have data like this:
[
  { numbers: [1] },
  { numbers: [1,2] },
  { numbers: [1,2,3] },
  { numbers: [2,3] },
  { numbers: [3] }
]

and I would filter with: "{where: {number:{inq: [1]}}}" it would just return: [ { numbers: [1] } ]
because it compares the WHOLE numbers content with each of the entries and so just the first one matches.

What I want is a search like: "{where: {number:{inarray: 1}}}"
and then it should return me each object in which the number 1 is in the numbers array. So I would get:
[
  { numbers: [1] },
  { numbers: [1,2] },
  { numbers: [1,2,3] }
]

Thanks!

Raymond Feng

unread,
Oct 24, 2014, 12:49:48 AM10/24/14
to Jan Oberhauser, loopb...@googlegroups.com
What DB do you have? We don’t support comparison on embedded array or object for relational DBs.

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

Jan Oberhauser

unread,
Oct 24, 2014, 1:10:16 AM10/24/14
to loopb...@googlegroups.com
Ah ok thanks. That explains then why I could not find anything.

Still surprised that it did not come up earlier because it seems like something that is needed a lot.

Currently for development I am simply using the memory one, but will switch the next days to Mongo which will get used in production.

Alfredo Rivera

unread,
Aug 4, 2015, 1:09:49 PM8/4/15
to LoopbackJS
Is this feature ever going to be implemented?
You know, there is people out there that does not uses MongoDB :(

Sundeep Gupta

unread,
Jun 28, 2016, 10:04:43 AM6/28/16
to loopb...@googlegroups.com, jan.obe...@gmail.com, ray...@strongloop.com
Hi, Raymond, I need the exact same type of query as Jan. My example is like this:

Message model has a property `ccountIds` of type Array. If I have `myAccountId`, I'd like to fetch all the messages where `myAccountId` exists in the property `accountIds`. I can't find the syntax to accomplish this. 

Sundeep Gupta

unread,
Jun 28, 2016, 10:22:40 AM6/28/16
to LoopbackJS, jan.obe...@gmail.com, ray...@strongloop.com
I just found the answer. The `where` should look like this:

`{accountIds: myAccountId}`

Reply all
Reply to author
Forward
0 new messages