Declare array into searchFields Worklight JSONStore

核能气质少年 提交于 2019-12-11 11:45:40

问题


I have a JSON object to my collection with JSONStore like this:

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

But, how is possible declare the industry field into searchFields?, in order to search a pattern in the array.

Cheers


回答1:


There's no array type for search fields. You can only index values in objects that are string, boolean, number and integer.

You could change:

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

to:

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

and then use the following search field: {'industry.name' : 'string'}. That will enable you to do something like WL.JSONStore.get('collection').find({'industry.name' : 'Banking'}, {exact: true}) and get back an object like this one:

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

This is documented under the search field section of general terminology in the documentation here.

That would mean writing code like this to change the data being added to the collection:

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

Alternatively, you could also change it into a string:

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

and get back something like this:

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

Then you can use the search field {industry : 'string'} and do something like WL.JSONStore.get('collection').find({industry: 'Energy'}, {exact: false}) to get objects that have Energy somewhere in the industry value string.

FYI - Feature requests here.



来源:https://stackoverflow.com/questions/25353874/declare-array-into-searchfields-worklight-jsonstore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!