How to use a search bar Filter for nested array objects in ionic 4 search bar
ionic version-4.7.1 below I have attached my Filter function but I couldn't use this function to filter objects inside a nested array
this.filterData = this.allData.filter(data => {if (data.letter && this.searchTerm) {
for (var i = 0; i < data.child_contacts.length; i++) {
if (
data.child_contacts[i].full_name
.toLowerCase()
.indexOf(this.searchTerm.toLowerCase()) > -1
) {
return (
data.child_contacts[i].full_name
.toLowerCase()
.indexOf(this.searchTerm.toLowerCase()) > -1
);
}
}
return false;}});
here is data that needed to be filtered
this.allData=[ {
"letter":"A",
"child_contact":[
{"full_name":"First"},
{"full_name":"second"},
{"full_name":"third"}
]
},
{
"letter":"B",
"child_contact":[
{"full_name":"First second"},
{"full_name":"Third"}
]
},
{
"letter":"C",
"child_contact":[
{"full_name":"Third"}
]
} ]
if I search 'First' my expected output would be,
[ {
"letter":"A",
"child_contact":[
{
"full_name":"First"
},
]
},
{
"letter":"B",
"child_contact":[
{
"full_name":"First"
},
]
}]
I'm not sure if this is the complete solution but one problem I can see is you are using child_contacts with an s in the filter,
for (var i = 0; i < data.child_contacts.length; i++) {
but the data is child_contact:
this.allData=[ {
"letter":"A",
"child_contact":[
{"full_name":"First"},
{"full_name":"second"},
{"full_name":"third"}
]
},
Related
i have data in mongodb,just like
{
"name":"test1",
"receivedDate":"2021-05-18 00:00:52",
}
{
"name":"test2",
"receivedDate":"2021-05-18 00:00:52",
}
{
"name":"test3",
"receivedDate":"2021-05-18 00:00:52",
}
{
"name":"test4",
"receivedDate":"2021-05-18 00:00:52",
}
I want to find data with format like:
{
"name":["test1","test2","test3","test4"],
}
When I use the following code, it can run
GroupOperation group = Aggregation.group().push("name").as("name");
And result like
[
{
"_id": null,
"name": [
"test1",
"test2",
"test3",
"test4",
"test4"
]
}
]
But when I use another code
GroupOperation group = Aggregation.group();
for (int i = 0; i < params.length; i++) {
group.push(params[i]).as(params[i]);
}
it get bad result
[
{
"_id": null
}
]
what should i do?
Thanks for your suggestion
MongoDB $group operator does not allow filtering (only accumulator operators).
Your second code is trying do something like this:
{
$group:{
_id:null,
test1:{
$push:"$test1"
},
test2:{
$push:"$test2"
},
...
}
}
and you get the wrong result
Solution: Filter before grouping (MongoPlayground)
MatchOperation match = Aggregation.match(Criteria.where("name").in(Arrays.asList(params)))
GroupOperation group = Aggregation.group().push("name").as("name");
I should use this code
group = group.push(params[i]).as(params[i]);
I need to compare two arrays to find whether all elements in the first array matches the second one.
First array:
var tasktime = [2,3,4];
Second array:
'working_days': [
{
'slots': [ 8, 9, 14, 15 ]
}
];
I need to check whether all the elements in the "tasktime" array exists in the "slots" array.
Below is the query I have tried but not getting the expected results.
var defaultCondition = [
{
query: {
"working_days": { $elemMatch: { slots: { $setIntersection: [ 'slots', tasktime ] } } }
}
}
];
db.GetAggregation('tasker', defaultCondition, function (err, taskers) {
if (err || !taskers[0]) {
res.send({ count: 0, result: [] });
} else {
callback(err, taskers);
}
});
Need someone's valuable help on this.
You need to use $all to find all the values in taskTime array.
You can simply do :
db.tasker.find({"working_days.slots" : {$all : taskTime}});
I have my mongodb document like this...
{
"semantics" : [
{
"text": "abc gave payment to xyz",
"action": "gave"
},
{
"text": "abc wanted quicker solution",
"action": "want"
}
],
"keyword" : [
{
"word":"payment",
"imp":0.91
},
{
"word":"solution",
"imp":0.7
}
]
}
The requirement is to find action values for those words whose importance is more than 0.9.
In the above case, payment has importance more than 0.9 and, hence, should be considered. payment exists in one of the array and the action value in it is gave.
I am requesting help in constructing mongodb query for the same.
You can first use mapReduce:
db.collection.mapReduce(
function() {
for (var i = 0; i < this.keyword.length; i++) {
if (this.keyword[i].imp >= 0.9) {
emit(this.keyword[i].word, this.semantics)
}
}
},
function(key, values) { },
{
out: {merge: 'result'},
finalize: function(key, semantics) {
var result;
for (var i = 0; i < semantics.length; i++) {
if (semantics[i].text.indexOf(key) != -1) {
result = {key: semantics[i].action};
}
}
return result;
}
)
We don't care about the reduce function here since map will return {word with imp >= 0.9: the whole semantics}.
Later, before storing the result into result collection, finialize function is called and it goes through all semantics associated with a word and fetches all actions related to texts that contain the key word.
After this, you can db.result.find() to see the result, you will see some null result because not all key has a matched text and action, you need to clean up a bit.
I have a trouble with mongodb data below.
I want to get data [projects][log][subject].
so, I tried like this
$project':{_id:0, projects.log.subject:1}
but it is not correct syntax..
{
"_id": ObjectID("569f3a3e9d2540764d8bde59"),
"A": "book",
"server": "us",
"projects": [
{
"domainArray": [
{
~~~~
}
],
"log": [
{
~~~~~,
"subject": "I WANT THIS"
}
],
"before": "234234234"
},
{
"domainArray": [
{
~~~~
}
],
"log": [
{
~~~~~,
"subject": "I WANT THIS"
}
],
"before": "234234234"
},....
] //end of projects
}//end of document
How can I get data group by [subject]? I have no idea about this..
Edited-
I expected data like this
{
"subject":"first",
"subject":"second",
"subject":"third",
"subject":"~~~"
}
Is it possible? I just want to get array of subject.
Not sure whether it is your expected result or not. Can you please try this:
db.project.aggregate([
{$project:{projects:1,_id:0}},
{$unwind:"$projects"},
{$unwind:"$projects.log"},
{$project:{subject:"$projects.log.subject",_id:0}}
])
and Map-Reduce for above result of word count is as below:
//map function
var map = function() {
for(var i in this.projects)
{
for(var j in this.projects[i].log)
{
var arrayWords = this.projects[i].log[j].subject.split(" ");
for(var k = 0; k < arrayWords.length; k++)
{
emit(arrayWords[k],{occurance:1});
}
}
}
};
//reduce function
function reduce(word, arrayOccurance) {
var totalOccurance = 0;
for (var i = 0; i < arrayOccurance.length; i++)
{
totalOccurance = totalOccurance + arrayOccurance[i].occurance;
}
return { occurance:totalOccurance };
}
//combine both function into operation and output result into wordOccurance collection
db.project.mapReduce(
map,
reduce,
{
query: {"projects.log.subject":"~~~~~"},
out: "wordOccurance"
}
)
//query the result output
db.wordOccurance.find()
You can change the query under mapreduce to match your subject that want to word count. Please let me know if my mapreduce function doesn't meet your expected result.
You can also refer below two pages to create and troubleshoot map and reduce function:
Troubleshoot the Map Function
Troubleshoot the Reduce Function
I stuck with 2 problems first how to loop and update in Mini MongoDb like this. And How to get _id for update it.
var data = [
{
"key" : "North America" ,
"values" : [
[ 1025409600000 , 23.041422681023] ,
[ 1028088000000 , 19.854291255832]
]
},
{
"key" : "Africa" ,
"values" : [
[ 1025409600000 , 7.9356392949025] ,
[ 1028088000000 , 7.4514668527298]
]
}];
And this function for loop BUT it just stop at very first looping
for(var i = 0; i < data.length; i++) {
Looping.insert({
key:data[i].key,
values:[]
});
var looping_id = Looping._id;
for(var j = 0; j < data[i].values.length; j++) {
Looping.update({
_id: looping_id
},
{
$addToSet: {values: data[i].values[j]}
});
}
}
Assuming Looping is a collection, you could insert your items with this:
_.each(data, function(item) {
Looping.insert(item);
});
However, because you are using $addToSet, I'm guessing that the values could be non-unique. In that case you can try:
_.each(data, function(item) {
var id = Looping.insert({key: item.key});
_.each(item.values, function(value) {
Looping.update(id, {$addToSet: {values: value}});
});
});
Notes:
The id of the inserted item is returned by the call to insert, it is not held in the collection object.
If you have a lot of items to insert, you should consider de-duplicating each set of values prior to the insert so you can avoid all of the updates.