将数组声明为searchFields Worklight JSONStore

时间:2014-08-17 21:37:52

标签: arrays search field ibm-mobilefirst jsonstore

我的JSONStore集合中有一个JSON对象,如下所示:

{
    name : 'name1',
    industry : ['Banking', 'Energy', 'Insurance', 'Media', 'Retail', 'Telco', 'Travel'],
    buyer : ['CMO'],
    link : 'foo.com' 
}

但是,如何将行业字段声明为searchFields?,以便搜索数组中的模式。

干杯

1 个答案:

答案 0 :(得分:4)

搜索字段没有array类型。您只能为stringbooleannumberinteger的对象中的值编制索引。

你可以改变:

{ industry : ['Banking', 'Energy'] }

为:

{ industry : [{name: 'Banking'}, {name: 'Energy'}] }

然后使用以下搜索字段:{'industry.name' : 'string'}。这将使您能够执行WL.JSONStore.get('collection').find({'industry.name' : 'Banking'}, {exact: true})之类的操作并返回类似这样的对象:

[{_id: ..., json: {name: ..., industry: [..., {name: Banking}, ...], buyer: ..., link: ...}}]

这是在文档here中的一般术语的搜索字段部分中记录的。

这意味着编写这样的代码来更改添加到集合中的数据:

var output = [];
['Banking', 'Energy', 'Insurance', 'Media'].forEach(function (element) {
    output.push({name: element});
});
console.log( JSON.stringify(output, null, ' ') ); 

或者,您也可以将其更改为字符串:

{industry : ['Banking', 'Energy', 'Insurance', 'Media'].toString() }

并取回这样的东西:

{industry : "Banking,Energy,Insurance,Media"}

然后,您可以使用搜索字段{industry : 'string'}并执行WL.JSONStore.get('collection').find({industry: 'Energy'}, {exact: false})之类的操作,以获取Energy值字符串中某处industry的对象。

仅供参考 - 功能请求here