Using jq I would like to conditionally update the objects in the "folders" array.
For each object which contains "cand_name":"folderA" a new attribute "files" which has the value of compare.files should be added
{
"tmp":{
"folders":[
{
"ref_name":"folderB",
"cand_name":"folderA"
},
{
"ref_name":"folderC",
"cand_name":"folderA"
},
{
"ref_name":"folderC",
"cand_name":"folderE"
}
],
"compare":{
"files":[
{
"candidate":"Z3S1"
}
]
}
}
}
For the above input the expected output should be
{
"tmp":{
"folders":[
{
"ref_name":"folderB",
"cand_name":"folderA"
"files":[
{
"candidate":"Z3S1"
}
]
},
{
"ref_name":"folderC",
"cand_name":"folderA"
"files":[
{
"candidate":"Z3S1"
}
]
},
{
"ref_name":"folderC",
"cand_name":"folderE"
}
],
"compare":{
"files":[
{
"candidate":"Z3S1"
}
]
}
}
}
The key to a simple solution is |=. Using the expected output as a guide to the intended requirements, one could write:
.tmp.compare.files as $f
| .tmp.folders
|= map( if .cand_name=="folderA"
then .files = $f else . end)
Related
I am trying to update the ordered quantity of the first article of the first order.
Insert and update instructions:
db.client.insertOne({
"noClient":1, "nomClient":"John Doe", "noTéléphone":"1234567890",
"commandes":[
{
"noCommande":1, "dateCommande":"22-11-2022",
"lignesCommande":[
{ "article":{"noArticle":1, "description":"Lenovo ThinkPad", "prixUnitaire":12000000, "quantiteEnStock":250}, "quantite":13 },
{ "article":{"noArticle":2, "description":"Iphone14", "prixUnitaire":16000000, "quantiteEnStock":123}, "quantite":2 },
{ "article":{"noArticle":3, "description":"All star shoes", "prixUnitaire":12500, "quantiteEnStock":15}, "quantite":1 },
{ "article":{"noArticle":4, "description":"Cahier 200pages", "prixUnitaire":12000, "quantiteEnStock":27}, "quantite":2 }
]
},
{
"noCommande":2, "dateCommande":"23-11-2022",
"lignesCommande":[
{ "article":{"noArticle":5, "description":"Airpods", "prixUnitaire":1300000, "quantiteEnStock":13}, "quantite":1 },
{ "article":{"noArticle":4, "description":"Cahier 200pages", "prixUnitaire":12000, "quantiteEnStock":23}, "quantite":1 }
]
}
]
});
db.client.updateOne({"commandes.noCommande":1, "commandes.lignesCommande.article.noArticle" :1},
{"$set" : {"commandes.lignesCommande.$.quantite":1}})
Screenshot of code:
Following command doesn't work:
db.client.updateOne({"commandes.noCommande":1, "commandes.lignesCommande.article.noArticle" :1},
{"$set" : {"commandes.lignesCommande.$.quantite":1}})
This is actually a pain. Mongodb does not allow multiple positional operators (meaning you cannot use $ directly within your query). Instead, you could use positional filters with arrayFilters.
Playground example - https://mongoplayground.net/p/tDGYeNIYco4
db.collection.update({
"commandes.noCommande": 1
},
{
$set: {
"commandes.$.lignesCommande.$[lig].quantite": 100
}
},
{
arrayFilters: [
{
"lig.article.noArticle": 1
}
]
})
We are running MuleEsb 3.9.0
I'm trying to remove empty array's in my dataweave transformation. For example:
payload map ((value , indexOfValue) -> {
value : {
content: value.content,
subvalue: value.subValue map ((subValue, indexOfsubValue)->
{
sub: subValue
}) filter ($.sub != null )
}
})
this will result in
[
{
value:
{
content: xyz
subValue: []
}
}
]
i want subValue to be totally removed. to be outputted:
[
{
value:
{
content: xyz
}
}
]
You need something like this:
payload map ((v) -> {
value: {
content: v.content,
(subvalue: v.subValue map ... ) when ((sizeOf v.subValue) != 0)
}
})
I want to get the translatedText value. But in the console.log keep show undefined. w
this.getPosts('Hello', 'ja');
getPosts(text, category) {
this.translateService.getPosts(text, category).subscribe(response => {
console.log(response.data.translations.translateText);
this.items = response.data.translations;
console.log(items.translateText)
}
Here's the Json Format.
{
"data": {
"translations": [
{
"translatedText": "Hallo Welt",
"detectedSourceLanguage": "en"
}
]
}
}
I have the following document:
{
"_id":"575322d9585095d9929554ba",
"Level1":{
"Level2":[
{
"Level3a":{
"Level4":{
"Level5":{
"name":"John",
"surname":"Matthew"
}
}
}
},
{
"Level3a":{
"Level4":{
"Level5":{
"name":"Emma",
"surname":"Jackson"
}
}
}
}
]
}
}
I need to insert the new name and surname at Level5. I tried the $push method but I got the error that the dotted field .. is not valid for storage:
db.names.update({ "_id":"575322d9585095d9929554ba" },
{
$push: {
"Level1.Level2":
{ $each: [ { "Level3a.Level4.Level5.name": "Greg" },
{ "Level3a.Level4.Level5.surname": "Cook" }] } } } )
It seems that push does not allow the inserting new data in hierarchical arrays or I am wrong?
There is an issue with your query as $each is used to add multiple values to same array....
As we are going to add only one entry to array we don't need to use $each
so query looks like this:
db.vnenad.update({
"_id" : "575322d9585095d9929554ba"
},
{
$push : {
"Level1.Level2" : {
"Level3a" : {
"Level4" : {
"Level5" : {
"name" : "johnny",
"surname" : "rambo"
}
}
}
}
}
})
as I was debbuging your query I decided to create variable with document to insert inside an array, then it was easier for me to adjust your query - please see below:
var doc = {
"Level3a" : {
"Level4" : {
"Level5" : {
"name" : "johnny",
"surname" : "rambo"
}
}
}
}
db.vnenad.update({
"_id" : "575322d9585095d9929554ba"
},
{
$push : {
"Level1.Level2" : doc
}
})
I am looking for a logstash filter that can modify array fields.
For example, I would like a modifier that can turn this JSON document
{
arrayField: [
{
subfield: {
subsubfield: "value1"
}
},
{
subfield: {
subsubfield: "value2"
}
}
]
}
Into this JSON document
{
arrayField: [
{
subfield: "value1"
},
{
subfield: "value2"
}
]
}
I have tried the following input
input {
mutate {
replace => ["[arrayField][subfield]", "%{[arrayField][subField][subsubField]}"]
}
}
but the input just rewrites the array field instead of operating on each element of the array. How do you set up a modifier to operate on each element of an array?
Thanks Alain Collins for pointing out the ruby filter. The below input did the trick.
input {
ruby {
code => "
event['arrayField'].each{|subdoc| subdoc['subfield'] = subdoc['subfield']['subsubfield']}
"
}
}