【问题标题】:Declare array into searchFields Worklight JSONStore将数组声明到 searchFields Worklight JSONStore
【发布时间】:2014-10-10 19:26:05
【问题描述】:

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

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

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

干杯

【问题讨论】:

    标签: arrays search field ibm-mobilefirst jsonstore


    【解决方案1】:

    搜索字段没有 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}) 之类的操作来获取在industry 值字符串中某处具有Energy 的对象。

    仅供参考 - 功能请求here

    【讨论】:

      猜你喜欢
      • 2013-01-13
      • 2013-02-15
      • 2013-08-13
      • 2014-07-26
      • 2014-08-10
      • 2014-09-19
      • 2013-02-14
      • 2014-06-29
      • 2013-12-07
      相关资源
      最近更新 更多