产品架构具有许多详细信息,例如:productName,生产日期,制造商名称,产品类型等。但是我需要支持为产品详细信息没有定义架构的灵活性。下面是Realm-JS模式。
import Realm from 'realm';
export default class ProductModel extends Realm.Object { }
ProductModel.schema = {
name: 'ProductModel',
properties: {
productAttributes: { type: 'list', objectType: 'ProductDetailsModel' },
ID: { type: 'string', optional: true },
}
};
import Realm from 'realm';
export default class ProductDetailsModel extends Realm.Object { }
ProductDetailsModel.schema = {
name: 'ProductDetailsModel',
properties: {
attributeName: { type: 'string', optional: true },
attributeValue: { type: 'string', optional: true },
}
};
这种灵活性导致编写过滤器查询的复杂性。我需要能够根据特定的产品属性名称和产品属性值进行过滤。
let query = "productAttributes.attributeName = " + "\"" + "Manufacturer" + "\"" + " AND productAttributes.attributeValue = [c]" + "\"" + "Pepsico" + "\"";
var productList = this.realm.objects('ProductModel').filtered(query).snapshot();
请帮助我编写查询,以从与attributeName和attributeValue都匹配的ProductDetailsModel的数据库中过滤出ProductModel?
当前查询与单个ProductDetailsModel不匹配,其中attributeName =“ Manufacturer” AND attributeValue =“ Pepsico”
答案 0 :(得分:0)
您通过“对象”本地属性而不是链接对象的名称来引用它。例如
let attributeName = "Manufacturer"
let attributeValue = "Pepsico"
let query = `productAttributes.attributeName ==[c] '${attributeName}' AND productAttributes.attributeValue ==[c] '${attributeValue}'`