1

I'm trying to use nativescript-couchbase-plugin. Here is my code:

whereArray.push({ property: "type_id", comparison: "equalTo", value: typeId });
return itemsDb.query({
     select: [],
     where: whereArray,
     order: [{property: "rating", direction: "desc"}],
     limit: this.PAGE_SIZE,
     offset: page * this.PAGE_SIZE
});

Everything works fine: condition in where clause, ordering, limit and offset. But I want to add something new to my condition and I'm trying to do this:

whereArray.push({ property: "title", comparison: "like", value: "some name"});
or
whereArray.push({ property: "filters", comparison: "in", value: [1,2,3]});

And these cases don't work. Neither of them. The only one that works is the first one with type_id

So the question is - how to use where array to query corresponding data?

Data Sample:

{
    "type_id": 2,
    "title": "some name",
    "regionId": 372,
    "uid": 16177,
    "filters": [
        1,
        2,
        3
    ]
},

P.S. There is an issue inside original repository. But it has no activity at all.

AxelPAL
  • 1,047
  • 3
  • 12
  • 19

1 Answers1

1

I must agree the plugin documentation could be better. But if you go through the TypeScript declaration, you will find the issue with your code.

When you are adding multiple where conditions, you must include a logical operator.

 import {
    Couchbase,
    QueryLogicalOperator,
    QueryComparisonOperator
 } from "nativescript-couchbase-plugin";


const database = new Couchbase("my-database");

// Insert docs for testing
const documentId = database.createDocument({
    type_id: 2,
    title: "some name",
    regionId: 372,
    uid: 16177,
    filters: [1, 2, 3]
});
console.log(documentId); 

// Query
const results = database.query({
    select: [],
    where: [
        {
            property: "type_id",
            comparison: "equalTo",
            value: 2,
            logical: QueryLogicalOperator.AND
        },
        {
            property: "filters",
            comparison: "in",
            value: [1, 2, 3]
        }
    ],
    order: [{ property: "rating", direction: "desc" }],
    limit: 10
});
console.log(results);
Manoj
  • 21,753
  • 3
  • 20
  • 41
  • I've added it. And I solved my problem with logical operators, but not with subquery: I want to search only [1] in document that has field filters: [1, 2, 3]. Also I haven't find out a way to use fuzzy search via this plugin. I tried 'like' operator with * and without it. – AxelPAL Mar 06 '20 at 06:24
  • I'm not sure I get what you mean. After adding logical operator the queries in your OP works. – Manoj Mar 06 '20 at 06:26
  • what about searching subquery in Couchbase? – AxelPAL Mar 09 '20 at 19:37
  • 1
    Atention: the logical operator must be on the SECOND condition! (At least for me, only worked that way - on Nativescript-vue - Android) – ainos Oct 15 '20 at 13:38