Dataweave remove empty array - arrays

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)
}
})

Related

ES6 : Create objects in a given format from array of key values

var result = {445: "L005.0", 455: "L006.0", 456: "L007.0", 457: "L008.0", 458: "L009.0", 459: "L027.0", 467: "L005.7", 580: "L001.0", 581: "L002.0", 587: "L003.0"};
From this "result", I want to output an object like this
{
"445": {
name: result[445],
icon: "fa-search"
},
"455": {
name: result[455],
icon: "fa-search"
},
"456": { ... },
"457": { ... },
...
...
}
So you need to iterate over keys to construct new object o;
let res={}; //initializing
for(let i of Object.keys(result))
{
res[i]={
"name": result[i],
"icon":"fa-seach"
}
}
console.log(res) //to test

How to update a multiple objects in an array with jq

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)

$push in MongoDb not working?

my schema looks like this:
var exampleSchema = newSchema({
profile:{
experience :[{
exp : String
}]
}
});
this is the codes to update experience in profile collection:
exampleSchema.statics.experience = function (id,experience, callback){
var update = {
$push: {
'profile.experience': experience
}
}
this.findByIdAndUpdate(id,update,function(err) {
if (err) {
callback(err);
} else {
callback(null);
}
})
I was getting error like The field 'profile.experience' must be an array but is of type String in document {_id: ObjectId('5653f1d852cf7b4c0bfeb54a')}[object Object]
console.log(experience) is equal to
{ exp: 'jlkjlkjlk' }
my collection should look like this:
experience:[
{
exp : "YYYY"
},
{
exp:"xxxx"}
]
Imagine that you have this collection:
/* 1 */
{
"_id" : ObjectId("565425e862760dfe14339ba8"),
"profile" : {
"experience" : [
{
"exp" : "Experto"
}
]
}
}
/* 2 */
{
"_id" : ObjectId("565425f562760dfe14339ba9"),
"profile" : {
"experience" : {
"exp" : "Experto"
}
}
}
/* 3 */
{
"_id" : ObjectId("5654260662760dfe14339baa"),
"profile" : {
"experience" : "Experto"
}
}
If you try (update doc /* 2 */):
db.profile.update(
{ _id: ObjectId("565425f562760dfe14339ba9") },
{ $push: { "profile.experience" : { exp : "Intermediate" } } }
)
You get this error:
The field 'profile.experience' must be an array but is of type Object
in document {_id: ObjectId('565425f562760dfe14339ba9')}
And if you try (update doc /* 3 */):
db.profile.update(
{ _id: ObjectId("5654260662760dfe14339baa") },
{ $push: { "profile.experience" : { exp : "Intermediate" } } }
)
You will get:
The field 'profile.experience' must be an array but is of type String
in document {_id: ObjectId('5654260662760dfe14339baa')}
i changed Schema like this
experience : [{type:String,exp:String}],
my update object looks like this
var update = {
$push: {
'profile.experience': san.exp
}
};
san looks like this :{ exp: 'YYY' }
Inside mongoose collectionlooks like this used RoboMongo
"experience" : [
"experienced in XXX",
"YYY"
],
$push: {
'profile.experience': experience
}
Remove .exp.
First you have to check you declared your field as an array like this(look at field products):
shop = {
'name': "Apple Store",
'description': "",
'direction': "",
'contact': "",
'products':[]
}
Now if you want to add something to the field products using $push
product = {
'name': "Iphone 6",
'description': "Iphone model 6, 64GB",
'price': 700,
'count': 3
}
myquery = { "name" : "Apple Store" }
obj ={"$push":{"products":{"$each": [product]}}}
db.collection.update_one(myquery,obj)
This code is provided for PyMongo framework. To use in MongoDB directly replace update_one by update. Mongo resource
You may use $set instead of $push which might work.
$set: {
'profile.experience': experience
}
are you searching for adding multiple values into single field then use this one.
write this one your model or schema:
arrayremarks:[{remark: String}]
then write in your controller:
module.exports.addingremarks = (req, res) => {
let casenum=JSON.parse(JSON.stringify(req.body.casenum).replace(/"\s+|\s+"/g,'"'))
var rem={remark:"Suman macha"}
Inwart.update( { 'casenum': casenum },{ $push: { arrayremarks:rem} } ,function (err, inwarts) {
if (err)
return console.error(err);
res.send(inwarts);
}
)
}

Transform array of objects in Ruby

I have JSON with one object for each site's traffic for 3 dates.
I want to merge these three objects together on "Date". Example below.
I'm using Ruby. What is the easiest way to do this?
Start JSON:
[
{
"Google":[
{
"Date":"2015-01-01",
"Value":100
},
{
"Date":"2015-02-01",
"Value":200
},
{
"Date":"2015-03-01",
"Value":300
}
]
},
{
"Yahoo":[
{
"Date":"2015-01-01",
"Value":1200
},
{
"Date":"2015-02-01",
"Value":1300
},
{
"Date":"2015-03-01",
"Value":1400
}
]
},
{
"Bing":[
{
"Date":"2015-01-01",
"Value":500
},
{
"Date":"2015-02-01",
"Value":600
},
{
"Date":"2015-03-01",
"Value":700
}
]
}
]
End JSON:
[
{
"Date":"2015-01-01",
"Google":100,
"Yahoo":1200,
"Bing":500
},
{
"Date":"2015-01-02",
"Google":200,
"Yahoo":1200,
"Bing":600
},
{
"Date":"2015-01-03",
"Google":300,
"Yahoo":1400,
"Bing":700
}
]
result = array.inject({}) do | a, e |
site, data = e.first
data.each do | x |
a[x[:Date]] ||= {}
a[x[:Date]][site] = x[:Value]
end
a
end
gives you a hash with the dates as keys. This can be transformed to the array by:
result.map { | k, v | v.update(:Date => k) }
Assuming exactly this structure, what might work is
results = []
your_array[0]['Google'].each_with_index do |item, index|
date = item['Date']
provider_values = your_array.inject({}) do |memo, current|
provider = current.keys[0]
value = current[provider][index]['Value']
memo[provider] = value
memo
end
results.push({'Date' => date}.merge(provider_values))
end
I'm currently on Windows so I can't be 100% sure of correctness, however fixing any syntax errors should be easy.

Push to array within subdocument in mongoose

I'd like to push to an array that's within a subdocument in Mongoose/MongoDB.
Here is the schema:
var UsersSchema = new mongoose.Schema({
user: String,
stream: String,
author: String,
tags: Array,
thumb: Number,
added: Number
})
var ContentSchema = new mongoose.Schema( {
title: String,
url: String,
description: String,
text: String,
users: [ UsersSchema ]
})
I'd like to push an array into the UserSchema.tags array for a specific users sub-document.
I have tried this and several variations: https://stackoverflow.com/a/19901207/2183008
By default, my front-end Angular app is sending the tags as an array of objects. So it's
[ { 'text': TAG_STRING_HERE } ]
or
[ { 'text': TAG_STRING_HERE }, { 'text': TAG2_STRING_HERE } ]
But I've also tried just using and array of strings, which I'm fine doing if objects are a problem for some reason.
I have tried this:
var tags = req.body.tags,
contentId = mongoose.Types.ObjectId( req.body.id )
Content.update(
{ 'users._id': contentId },
{ $push: { 'users.tags': { $each: tags } } }
).exec()
.then( function ( result ) {
console.log( result )
resolve( result )
}, function ( error ) {
if ( error ) return reject( error )
})
Which gives me this error:
{ [MongoError: cannot use the part (users of users.tags) to traverse the element ({users: [ { user: "54f6688c55407c0300b883f2", added: 1428080209443.0, stream: "watch", _id: ObjectId('551ec65125927f36cf4c04e9'), tags: [] }, { user: "54f6688c55407c0300b883f2", added: 1428080222696.0, stream: "listen", _id: ObjectId('551ec65e25927f36cf4c04ea'), tags: [] } ]})]
name: 'MongoError',
code: 16837,
err: 'cannot use the part (users of users.tags) to traverse the element ({users: [ { user: "54f6688c55407c0300b883f2", added: 1428080209443.0, stream: "watch", _id: ObjectId(\'551ec65125927f36cf4c04e9\'), tags: [] }, { user: "54f6688c55407c0300b883f2", added: 1428080222696.0, stream: "listen", _id: ObjectId(\'551ec65e25927f36cf4c04ea\'), tags: [] } ]})' }
The solution is below. Note this is using Q and Express. The part of note is the 'users.$.tags. I thought I had tried this but I guess not! I also used $pushAll instead, but $each might also work. My tags is always an array.
var tags = req.body.tags,
contentId = mongoose.Types.ObjectId( req.body.id )
console.log( tags )
Content.update(
{ 'users._id': contentId },
{ $pushAll: { 'users.$.tags': tags } }
).exec()
.then( function ( result ) {
console.log( result )
resolve( result )
}, function ( error ) {
if ( error ) return reject( error )
})

Resources