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
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.
Related
Say I have a model like:
$scope.types = [
{
name: 'X1500',
value: 'X1500',
tags: ['GRE','GRB']
},
{
name: 'VSH',
value: 'VSH',
tags: ['GRE','GRB']
}
]
And I want to filter all types that has GRE in their tags. Something like:
"type.value as type.name for type in types | filter:types.tags='GRE'"
How can I do this ?
Apparently, the filter mentioned in the question does work :)
i had an array like this:
arr = [
{ID: 502, Description: 'aaa', code: 1122},
{ID: 2, Description: 'bbb', code: 2211},
{ID: 700, Description: 'ccc', code: 2222}
];
when i try to filter the ID I get all occurences of the specific number:
$(filter)('filter')( arr, { ID: 2 } )[0]
returns entry one ID: 502 but it should return the entry with ID: 2
Where is my fault?
According to the docs when used with an object it will match the element if it contains the value.
A pattern object can be used to filter specific properties on objects contained by array. For example {name:"M", phone:"1"} predicate will return an array of items which have property name containing "M" and property phone containing "1".
There is a second option comparator passing true will cause it to perform a strict equality meaning it should only return exact matches.
$filter('filter')( arr, { ID: 2 }, true);
Fiddle: https://jsfiddle.net/enxbpjg0/
You could use a function instead of the object. So...
$filter('filter')(arr, function(value) {
return value.ID === 2;
});
Does the built in Angular "filter" support filtering an array in the sense that "filter where array contains "
Such as follows:
$scope.currentFilter = "Allentown";
$scope.users = [{
name: "user1",
locations: [
{ name: "New York", ... },
{ name: "Allentown", ... },
{ name: "Anderson", ... },
]
}, ... ];
<div ng-repeat="user in users | filter : { locations: { name: currentFilter } }"></div>
In other words I'm looking to filter to only users with a "locations" array that CONTAINS a location that matches the string by name.
If not, how can I accomplish this with a custom filter?
Your code snippet works as is since Angular 1.2.13. In case you're using an older version (tested on 1.0.1) and don't need to reuse your filtering routine across controllers (in which case you should declare a proper filter), you can pass the built-in filter a predicate function :
<div ng-repeat="user in users | filter : hasLocation">{{user.name}}</div>
And write something like this :
$scope.currentFilter = "Allentown";
$scope.hasLocation = function(value, index, array) {
return value.locations.some(function(location) {
return location.name == $scope.currentFilter;
});
}
Fiddle here.
I have users' collection whose schema is like:
{
_id: unique number,
name: 'asdf',
age: '12',
gender: 'm',
address: [
{area: 'sdf',
city: 'sdq',
state: 'wfw'},
{area: 'asdf',
city: 'sdfs',
state: 'vfdwd'}
]
}
I want to find out the users for whom all the values of state in address should be the value I pass. If even one of the state value doesn't match with the value I pass the user shouldn't be returned.
I tried simple find, aggregation framework with $unwind, $match but nothing seemed to get solution. Can you please help me out...
Thanks
P.S. please bear with multiple addresses for the sake of question. :)
To find out if all array entries match the state "wfw", do an aggregation like the following:
db.users.aggregate([
{ "$project" : {
"test" : {
"$allElementsTrue" : [{
"$map" : {
"input" : "$address",
"as" : "a",
"in" : { "$eq" : ["wfw", "$$a.state"] }
}
}]
}
} },
{ "$match" : { "test" : true } }
])
This aggregation takes each document, maps "state equals 'wfw'" over the address array to get a boolean array, and tests if the entire array is true, storing the result in `test, and then filtering the results based on test. You will need MongoDB 2.6 for support of some of the operators.
I don't know if I understand.
I replicated your document. When you want to retrieve an user by state you can do in many ways
If you search with single value you can do
db.g.find({ "address.state": "wfw" })
and retrieve an user
You can use $all
db.g.find( { "address.state": { $all: ["wfw","vfdwd"] } } ) // retrieve User
db.g.find( { "address.state": { $all: ["wfw","vfdwd","foo"] } } ) // don't retrieve User
or you can use $and
db.g.find( { $and: [ { "address.state":"wfw" },{ "address.state":"vfdwd" }] } )
But I don't know if I understand your question
Update and the correct answer
db.g.find( { "address.state": { $nin: ["wfw"] } } )
Let me Know
How do I get a more complex sort on a query, I have this query currently:
var store = Ext.create('Rally.data.custom.Store',{
data: changes,
limit: 'Infinity',
pageSize: 5000,
sorters: [
{
property: 'ReleaseScope',
direction: 'ASC'
},
{
property: 'ScheduleState',
direction: 'DESC'
}
]
});
Because the ScheduleState is hydrated I can't sort by the normal numerics, can I define the order using some kind of matcher?
i.e. say I want to show in order [Accepted, Completed, In-Progress, Defined, Backlog]
And, if I wanted to complicate this further and show stories with story points first, something like
All stories with a story point value != 0
Sorted by schedulestate [accepted, completed, in-progress, defined etc..]
stories with no story point value
some other sort here possibly
You can pass a sorterFn rather than a property/direction combo to implement custom sort logic:
sorters: [
{
sorterFn: function(a, b) {
var scheduleStates = ['Accepted', 'Completed', 'In-Progress', 'Defined'],
aState = a.get('ScheduleState'),
aIndex = _.indexOf(scheduleStates, aState),
bState = b.get('ScheduleState'),
bIndex = _.indexOf(scheduleStates, bState);
return a - b;
}
}
]
The above function should sort them based on schedule state descending I think.