Filter by multilevel Deep Property - angularjs

I have a collection somewhat like mentioned below:-
[
{
rootGroup: "group1",
secondGroup: false,
items: [
{name:"Ram"},
{name:"Mohan"},
{name:"Shyam"},
]
},
{
rootGroup: "group2",
secondGroup: true,
secondLevelGroups:[
{
group: "gp1"
items: [
{name:"Ganesh"},
{name:"Sita"},
{name:"Gita"},
]
},
{
group: "gp2"
items: [
{name:"Soham"},
{name:"Vikas"},
{name:"Ashish"},
]
}
]
}
]
Now I want to filter on name. So for example if the filter value is "am" then the output should be like as below.
[
{
rootGroup: "group1",
secondGroup: false,
items: [
{name:"Ram"},`
{name:"Shyam"},
]
},
{
rootGroup: "group2",
secondGroup: true,
secondLevelGroups:[
{
group: "gp2"
items: [
{name:"Soham"},
]
}
]
}
]
I want to do it using angularjs filter

Have you tried the extension angular filter: https://github.com/a8m/angular-filter

Related

Copy array to another array in same collection

I want to copy items from admin to newAdmins if it does not exist in the newAdmins
Before:
[
{
_id: "60801199bf57265ed8b786bc",
admins: [
"Kenny"
"Abu"
"Raj"
],
newAdmins: [
"Kenny"
"Abu"
]
}
]
After:
[
{
_id: "60801199bf57265ed8b786bc",
admins: [
"Kenny"
"Abu"
"Raj"
],
newAdmins: [
"Kenny"
"Abu"
"Raj"
]
}
]
Searched online but could not find a simpler way to do it.
One option is:
db.collection.update({},
[
{
$set: {
newAdmins: {
$setUnion: [
"$newAdmins",
"$admins"
]
}
}
}
],
{multi: true})
See how it works on the playground example

Sort array by two fields in different levels

My input:
[
{
"nfStatusNotificationUri": "http://172.19.0.2:32672/callback/nnrf-nfm/v1/onNFStatusEventPost/4e0becf9-c3ec-4002-a32b-2e35b76469b2",
"subscrCond": {
"serviceName": "namf-evts"
},
"subscriptionId": "36bc52dfdbdd4044b97ef15684706205",
"validityTime": "2022-04-30T16:40:48.274Z",
"reqNotifEvents": [
"NF_DEREGISTERED",
"NF_PROFILE_CHANGED",
"NF_REGISTERED"
]
},
{
"nfStatusNotificationUri": "http://172.19.0.2:32672/callback/nnrf-nfm/v1/onNFStatusEventPost/5319def1-af0b-4b7b-a94e-b787e614c065",
"subscrCond": {
"serviceName": "nbsf-management"
},
"subscriptionId": "e2e904bb52ca4fd6b048841c83a4c38e",
"validityTime": "2022-04-30T16:40:48.26Z",
"reqNotifEvents": [
"NF_DEREGISTERED",
"NF_PROFILE_CHANGED",
"NF_REGISTERED"
]
},
{
"nfStatusNotificationUri": "http://172.19.0.2:32672/callback/nnrf-nfm/v1/onNFStatusEventPost/31dfe10b-4020-47bd-943e-a3e293086b29",
"subscrCond": {
"serviceName": "namf-comm"
},
"subscriptionId": "e508077fab4f4b8d9dd732176a3777b9",
"validityTime": "2022-04-30T16:40:48.273Z",
"reqNotifEvents": [
"NF_DEREGISTERED",
"NF_PROFILE_CHANGED",
"NF_REGISTERED"
]
}
]
I would like to sort it by "subscriptionId" and "serviceName".
I can sort by subscriptionId but I don't know how to specify serviceName to the following expression.
jq -S '.|=sort_by(.subscriptionId)|.[].reqNotifEvents|=sort |del(.[].subscriptionId, .[].validityTime, .[].nfStatusNotificationUri)'
You can parameterize sort_by by a list of keys like so:
sort_by(.subscriptionId, .subscrCond.serviceName)
Online demo

How to remove arrays inside array if a condition is met

I have an object schema that looks like this:
{
"_id":"ObjectId(""30t00594537da2r7awe083va"")",
"balances":{
"intraday":[
[
1630939075734,
1899.09
],
[
1630939435939,
1899.32
],
[
1632306730756,
0
],
[
1632306759376,
0
],
[
1632272012916,
1372.22
]
]
}
}
I want to remove all arrays within "balances.intraday" array whose the second element is equal to 0.
so my desired array will looks like this:
{
"_id":"ObjectId(""30t00594537da2r7awe083va"")",
"balances":{
"intraday":[
[
1630939075734,
1899.09
],
[
1630939435939,
1899.32
],
[
1632272012916,
1372.22
]
]
}
}
I tried to use the $pull command but it only removes the index and not the whole array.
======================= Edit =======================
Thanks to Tom Slabbaert the solution is as follows :
db._get_collection().update_many(
{},
[
{
"$set": {
"balances.intraday": {
"$filter": {
"input": "$balances.intraday",
"cond": {
"$ne": [
{
"$arrayElemAt": [
"$$this",
1
]
},
0
]
}
}
}
}
}
])
You should use pipelined updates for this, like so:
db.collection.updateMany(
{},
[
{
$set: {
"balances.intraday": {
$filter: {
input: "$balances.intraday",
cond: {
$ne: [
{
$arrayElemAt: [
"$$this",
1
]
},
0
]
}
}
}
}
}
])
Mongo Playground

Bool query in array field

I have a very particular issue concerning querying over a boolean field and a string field which are nested to an array field. The index mapping is as follow:
indexes :string_field_1, type: 'string'
indexes :string_field_2, type: 'string'
indexes :boolean_field_1, type: 'boolean'
indexes :array_field_1 do
indexes :boolean_field_2, type: 'boolean'
indexes :string_field_3, type: 'string'
end
indexes :array_field_2 do
indexes :integer_field_1, type: 'integer'
end
indexes :array_field_3 do
indexes :integer_field_2, type: 'integer'
end
The document index also has many other fields which are not nested to the array field, but have to be included among the query fields.
I have tried an approach using filter and bool queries that is as follow:
"query":
{"bool":
{"must":
[
{"query_string":
{"query":"text which is being searched",
"fields":[
"string_field_1",
"string_field_2",
"array_field_1.string_field_3"
],
"fuzziness":"1","analyze_wildcard":true,"auto_generate_phrase_queries":false,"analyzer":"brazilian","default_operator":"AND"}
}
],
"filter":[
{"bool":
{"must":
[
{"bool":
{"should":
[
{"term":{"boolean_field_1":false}},
{"terms":{"array_field_2.integer_field_1":[x,z]}},
{"term":{"array_field_3.integer_field_2":y}}]}},
{"bool":
{"should":
[
{"term":{"array_field_1.boolean_field_2":true}},
{"terms":{"array_field_2.integer_field_1":[x,z]}},
{"term":{"array_field_3.integer_field_2":y}}]}},
]
}
}
]
}
}
]
}
}
The problem with this query is that it is returning a document which, in my opinion, doesn't have to be returned.
The document, in this case, is the bellow:
_source": {
"string_field_1": "text 1",
"string_field_2": "text 2",
"boolean_field_1": false,
"array_field_1": [
{
"boolean_field_2": true,
"string_field_3": "some text which is not being searched"
},
{
"boolean_field_2": true,
"string_field_3": "some text which is not being searched"
},
{
"boolean_field_2": false,
"string_field_3": "text which is being searched"
},
{
"boolean_field_2": true,
"string_field_3": "some text which is not being searched"
}
],
"array_field_2": [
{
"integer_field_1": A
}
],
"array_field_3": [
{
"integer_field_2": B
}
]
}
As you can notice, the third item of array_field_1 contains boolean_field_2: false and also the text which is being searched. But, according to my filter: clause, only the documents which array_field_1.boolean_field_2 is true have to be retrieved, unless array_field_2.integer_field_1: or array_field_3.integer_field_1 occurs, which is not true, according to my query part.
It seems elastic is not considering that the array_field_1[2] is the one that the boolean_field_2 is false.
How can I make my query so that this document isn't retrieved?
Thanks is advance,
Guilherme
That was my solution:
"query":{
"bool":{
"should":
[
{
"query_string":
{
"query":"text which is being searched",
"fields":
[
"string_field_1",
"string_field_2"
],
"fuzziness":"1","analyze_wildcard":true,"auto_generate_phrase_queries":false,"analyzer":"brazilian","default_operator":"AND"
}
},
{
bool: {
should:[
{
query:{
nested: {
path: 'array_field_1',
query: {
bool: {
must: [
{ match: { "array_field_1.string_field_3": "text which is being searched"} },
{term: {"array_field_1.boolean_field_2": true}}
]
}
}
}
}
},
{
bool:
{
must: [
{
query:{
nested: {
path: 'movimentos',
query: {
bool: {
must: [
{ match: { "array_field_1.string_field_3": "text which is being searched"} },
{term: {"array_field_1.boolean_field_2": false
]
}
}
}
}
},
{
query: {
bool: {
should: [
{"terms":{"array_field_2.integer_field_1":[x,z]}},
{"term":{"array_field_3.integer_field_2":y}}
]
}
}
}
]
}
}
]
}
}
]
}
}
Another approach consists of putting the array_field_1.string_field_3 query together with the bool query related to the boolean field:
"query":{
"bool":{
"should":
[
{
"query_string":
{
"query":"text which is being searched",
"fields":
[
"string_field_1",
"string_field_2"
],
"fuzziness":"1","analyze_wildcard":true,"auto_generate_phrase_queries":false,"analyzer":"brazilian","default_operator":"AND"
}
},
{
"bool":{
"must":
[
{
"query_string":
{
"query":"text which is being searched",
"fields":["array_field_1.string_field_3"],
"fuzziness":"1","analyze_wildcard":true,"auto_generate_phrase_queries":false,"analyzer":"brazilian","default_operator":"AND"
}
},
{
"bool":{
"should":
[
{"term":{"array_field_1.boolean_field_2":true}},
{"terms":{"array_field_2.integer_field_1":[x,z]}},
{"term":{"array_field_3.integer_field_2":y}}
]
}
}
]
}
}
],
"filter":
[
{
"bool":{
"should":
[
{"term":{"boolean_field_1":false}},
{"terms":{"array_field_2.integer_field_1":[x,z]}},
{"term":{"array_field_3.integer_field_2":y}}
]
}
}
]
}
}
This query also retrieves the document, unfortunately. I really do not know how to build this query properly.
The query above is organized as:
(X) OR (A AND (B OR C OR D))

Reuse build condition for multiple targets in binding.gyp

Originally my binding file has only one target and that has been fine:
{
'targets': [
{
'target_name': 'target1',
'sources': [ 'source1.cc', 'source2.cc' ],
'cflags_cc!': [ '-fno-rtti', '-fno-exceptions' ],
'cflags!': [ '-fno-exceptions' ],
'conditions': [
#A very, very long condition
]
},
}
Now I need another target which is more or less the same, but build an executable file instead of link object. If I duplicate the original target, that will be OK, however I don't want to repeat the condition which is exactly the same. How could I do that?
E.g. My ideal bindin.gyp would look somewhat like this:
{
'conditions': [
#A very, very long condition
]
'targets': [
{
'target_name': 'target1',
'sources': [ 'source1.cc', 'source2.cc' ],
'cflags_cc!': [ '-fno-rtti', '-fno-exceptions' ],
'cflags!': [ '-fno-exceptions' ],
'conditions' : #Refer to the conditions stated above
},
{
'target_name': 'target2',
'type' : 'executable'
'sources': [ 'source1.cc', 'source3.cc' ],
'cflags_cc!': [ '-fno-rtti', '-fno-exceptions' ],
'cflags!': [ '-fno-exceptions' ],
'conditions' : #Refer to the conditions stated above
},
}
I tried using variables but node-gyp only allows variables of type string or list, while 'conditions' is an associative array
I'm not really sure, but at a guess, you might be able to stick the long condition array in a third target and depend on that condition target from each of the other two. Something like this:
{
'targets': [
{
'target_name': 'conditions_target',
'conditions': [
#A very, very long condition
]
},
{
'target_name': 'target1',
'sources': [ 'source1.cc', 'source2.cc' ],
'cflags_cc!': [ '-fno-rtti', '-fno-exceptions' ],
'cflags!': [ '-fno-exceptions' ],
'dependencies': [
'conditions_target',
],
},
{
'target_name': 'target2',
'type' : 'executable'
'sources': [ 'source1.cc', 'source3.cc' ],
'cflags_cc!': [ '-fno-rtti', '-fno-exceptions' ],
'cflags!': [ '-fno-exceptions' ],
'dependencies': [
'conditions_target',
],
},
}
But I'm not very experienced with node-gyp and that might blow things up.
More on dependencies here: https://gyp.gsrc.io/docs/UserDocumentation.md#Dependencies-between-targets
Thanks for suggestion from Joshua Skrzypek.
By my test, it can create a target with type 'none' for reusing build settings (including conditions, include_dirs, defines, ...) for not only other targets but also their direct or indirect dependents (using all_dependent_settings)! This is an example:
{
'targets': [
{
'target_name': 'all-settings',
'type': 'none',
'all_dependent_settings': {
'defines': [
'FOO',
],
'conditions': [
['OS=="mac"', {
'xcode_settings': {
'OTHER_LDFLAGS': [
'-undefined dynamic_lookup'
],
},
}],
['OS=="win"', {
'defines': [
'WIN32',
],
}, {
'defines': [
'OTHER',
],
}],
],
'include_dirs': [
'include',
],
'cflags': ['-Wall'],
'xcode_settings': {
'OTHER_CFLAGS': ['-Wall']
},
}
},
{
'target_name': 'mylib',
'type': 'shared_library',
'dependencies': [
'all-settings'
],
'sources': [
'foo.cc',
'bar.cc',
],
}
]
}
This method is used in my npm pacakges. See this for reference:
https://github.com/MrMYHuang/libxmljs/blob/ccbbea919e0718f45117481235e67185857c06e1/binding.gyp
and its dependent package:
https://github.com/MrMYHuang/node-libxslt/blob/49810b2269cb3c049a451bd3175eaad4d9de959c/binding.gyp#L10

Resources