-->

Declare array into searchFields Worklight JSONStor

2019-08-10 14: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.