StrongLoop Loopback Model find with OR condition on WHERE filter
Asked Answered
A

2

6

I'm trying to find a person by either their name OR identifier using StrongLoop's Model.find syntax (documentation here: http://apidocs.strongloop.com/loopback/#modelfindfilter-callback).

But I can't figure out how to specify the OR condition.

I can match both the name AND identifier using this syntax:

person.find({
    where: {
        'name.text': {like: 'Dave'},
        'identifier.value': {like: '3303927'}
    }
}, callback);

But I'd like to be able to match name OR identifier doing something like this:

person.find({
    where: {
        'name.text': {like: 'Dave'} ||
        'identifier.value': {like: '3303927'}
    }
}, callback);

Either this isn't supported or I can't get the syntax correct. It appears this is supported via the REST API (doc: http://docs.strongloop.com/display/DOC/Model+REST+API#ModelRESTAPI-Findmatchinginstances) so I'm hoping it's supported the way I'm trying to accomplish it.

Thanks!

Ariannearianrhod answered 20/5, 2014 at 21:58 Comment(0)
I
9

The AND/OR operator is added recently. Please see examples of usage at:

https://github.com/strongloop/loopback-datasource-juggler/blob/master/test/basic-querying.test.js#L156

The syntax is:

person.find({
    where: {
      or: {
        'name.text': {like: 'Dave'},
        'identifier.value': {like: '3303927'}
      }
    }
}, callback);
Iphagenia answered 20/5, 2014 at 22:48 Comment(1)
You have a typo there. You need to set the or clause like this: or: [ { 'name.text': {like: 'Dave'} }, { 'identifier.value': {like: '3303927'} } ]Coligny
G
2
person.find({
    where: {
        or: [
            {'name.text': {like: 'Dave'},
            {'identifier.value': {like: '3303927'}
        ]
     }
}, callback);
Germayne answered 1/8, 2017 at 9:3 Comment(1)
Please provide some explanation also, not just raw code.Mallis

© 2022 - 2024 — McMap. All rights reserved.