Syncano条件过滤多个字段

时间:2022-02-20 11:52:11

I'm trying to list the DataObjects in a class. I want to filter the list of DataObjects based on a conditional query on multiple fields. To select a DataObject based on field1 = xxx or field2 = xxx

我正在尝试在类中列出DataObjects。我想基于多个字段上的条件查询来过滤DataObjects列表。基于field1 = xxx或field2 = xxx选择DataObject

For eg: In class Inventory:

例如:在班级库存中:

+----------------------------------+
| Item  |  Quantity  |  Price      |
+----------------------------------+
| X     |    10      |    30       |
|                                  |
| Y     |    20      |    10       |
+----------------------------------+  

var filter = {
        'Quantity': { '_eq': 10 } OR
        'Price': { '_eq': 10 }
    };

Select Item with Quantity = 10 or Price = 10. How can achieve this?

选择数量= 10或价格= 10的项目。如何实现这一目标?

1 个解决方案

#1


2  

The best way to do this would be to break up your query into two separate calls and combine them. Syncano uses AND to combine queries, so for OR you would have to separate them into two separate API calls.

执行此操作的最佳方法是将查询分解为两个单独的调用并将它们组合在一起。 Syncano使用AND来组合查询,因此对于OR,您必须将它们分成两个单独的API调用。

// v1 Syncano JS SDK
var Syncano = require("syncano");  // CommonJS
var connection = Syncano({accountKey: "ACCOUNT_KEY"}); // or API Key
var DataObject = connection.DataObject;

var list = {instanceName: "INSTANCE_NAME", className: "CLASS_NAME"};
var filter1 = {
    'Quantity': { '_eq': 10 }
};
var filter2 = {
    'Price': { '_eq': 10 }
};

var QtyArray = [];
var PriceArray = [];

DataObject.please().list(list).filter(filter1).then(function(res){
    // res is an array of filter 'Quantity'
    QtyArray = res;
});

DataObject.please().list(list).filter(filter2).then(function(res){
    // res is an array of filter 'Price'
    PriceArray = res;
});

var comboArray = QtyArray.concat(PriceArray);

#1


2  

The best way to do this would be to break up your query into two separate calls and combine them. Syncano uses AND to combine queries, so for OR you would have to separate them into two separate API calls.

执行此操作的最佳方法是将查询分解为两个单独的调用并将它们组合在一起。 Syncano使用AND来组合查询,因此对于OR,您必须将它们分成两个单独的API调用。

// v1 Syncano JS SDK
var Syncano = require("syncano");  // CommonJS
var connection = Syncano({accountKey: "ACCOUNT_KEY"}); // or API Key
var DataObject = connection.DataObject;

var list = {instanceName: "INSTANCE_NAME", className: "CLASS_NAME"};
var filter1 = {
    'Quantity': { '_eq': 10 }
};
var filter2 = {
    'Price': { '_eq': 10 }
};

var QtyArray = [];
var PriceArray = [];

DataObject.please().list(list).filter(filter1).then(function(res){
    // res is an array of filter 'Quantity'
    QtyArray = res;
});

DataObject.please().list(list).filter(filter2).then(function(res){
    // res is an array of filter 'Price'
    PriceArray = res;
});

var comboArray = QtyArray.concat(PriceArray);