dataweave mapping from an array - mule

I have a basic dataweave mapping function. I want to have a number of objects with a simple array as input:
I have created the function generatePoints to achive this
fun generatePoints() = [1 to 24] map {
currentyear: 2023,
points: $[$$],
}
It returns a single array:
[
{
"currentyear": 2023,
"points": 1
}
]
But it should return something like this:
[
{
"currentyear": 2023,
"points": 1
},
{
"currentyear": 2023,
"points": 2
},
{
"currentyear": 2023,
"points": 3
}
.....
{
"currentyear": 2023,
"points": n
}
]
Does anybody know how to update the generatePoints function to achieve this ?

1 to 24 is already a range with type array. Therefore you have to remove the [] around it. As it is creating an Array with a single "array" element.

A little change of your solution:
%dw 2.0
output application/json
---
[1 to 24][0] map {
currentyear: 2023,
points: $,
}
Result:
[
{
"currentyear": 2023,
"points": 1
},
{
"currentyear": 2023,
"points": 2
},
{
"currentyear": 2023,
"points": 3
},
{
"currentyear": 2023,
"points": 4
},
{
"currentyear": 2023,
"points": 5
},
{
"currentyear": 2023,
"points": 6
},
{
"currentyear": 2023,
"points": 7
},
{
"currentyear": 2023,
"points": 8
},
{
"currentyear": 2023,
"points": 9
},
{
"currentyear": 2023,
"points": 10
} ...

Related

How to add a field in the mongoDB query representing a total figure

I have a mongodb query
[
{$group: {
_id: '$status',
count: {$sum: NumberInt(1)}
}}
]
The query returns the following result:
[
{ _id: "A", count: 22 },
{ _id: "B", count: 33 },
{ _id: "C", count: 44 }
]
Based on the above, I need to add a new field "totalCount" (e.g. 22 + 33 + 44 = 99) as in the following:
[
{ _id: "A", count: 22, totalCount: 99 },
{ _id: "B", count: 33, totalCount: 99 },
{ _id: "C", count: 44, totalCount: 99 }
]
Any help or clue is highly appreciated.
You have to make use of $facet stage to create multiple instances of aggregation, calculate the total count in a different stage and finally merge into a single output.
db.collection.aggregate([
{
"$facet": {
"totalCount": [
{
"$group": {
"_id": null,
"totalCount": {
"$sum": "$count"
}
},
},
],
"root": [
{
"$replaceRoot": {
"newRoot": "$$ROOT"
}
},
]
}
},
{
"$unwind": "$root"
},
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": [
"$root",
{
"$arrayElemAt": [
"$totalCount",
0
]
}
],
}
}
},
])
Mongo Sample Execution

How to parse json array in kusto query language

How to parse json array in kusto query language.
I have an output column which is having value in JSON array format as shown below. I Need to parse it to get values in form of two columns.
{"count": 14
"value": [
{
"Total_Record_Count": 16608,
"date": "2021-03-01T00:00:00Z"
},
{
"Total_Record_Count": 27254,
"date": "2021-02-24T00:00:00Z"
},
{
"Total_Record_Count": 6,
"date": "2021-02-01T00:00:00Z"
},
{
"Total_Record_Count": 26964,
"date": "2021-01-15T00:00:00Z"
},
{
"Total_Record_Count": 134516,
"date": "2020-12-18T00:00:00Z"
},
{
"Total_Record_Count": 27345,
"date": "2020-12-16T00:00:00Z"
},
{
"Total_Record_Count": 521,
"date": "2020-12-01T00:00:00Z"
},
{
"Total_Record_Count": 4,
"date": "2020-11-02T00:00:00Z"
},
{
"Total_Record_Count": 6,
"date": "2020-10-01T00:00:00Z"
},
{
"Total_Record_Count": 1,
"date": "2020-09-01T00:00:00Z"
},
{
"Total_Record_Count": 3,
"date": "2020-08-03T00:00:00Z"
},
{
"Total_Record_Count": 18,
"date": "2020-07-01T00:00:00Z"
},
{
"Total_Record_Count": 18754,
"date": "2020-06-16T00:00:00Z"
},
{
"Total_Record_Count": 4451898,
"date": "2020-06-08T00:00:00Z"
}
]}
How can I achieve it using output column name instead of using full json array.
Please see example below that uses mv-expand operator for breaking array into rows.
https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/mvexpandoperator
print d = dynamic(
{"count": 14,
"value": [
{
"Total_Record_Count": 16608,
"date": "2021-03-01T00:00:00Z"
},
{
"Total_Record_Count": 27254,
"date": "2021-02-24T00:00:00Z"
},
{
"Total_Record_Count": 6,
"date": "2021-02-01T00:00:00Z"
},
{
"Total_Record_Count": 26964,
"date": "2021-01-15T00:00:00Z"
},
{
"Total_Record_Count": 134516,
"date": "2020-12-18T00:00:00Z"
},
{
"Total_Record_Count": 27345,
"date": "2020-12-16T00:00:00Z"
},
{
"Total_Record_Count": 521,
"date": "2020-12-01T00:00:00Z"
},
{
"Total_Record_Count": 4,
"date": "2020-11-02T00:00:00Z"
},
{
"Total_Record_Count": 6,
"date": "2020-10-01T00:00:00Z"
},
{
"Total_Record_Count": 1,
"date": "2020-09-01T00:00:00Z"
},
{
"Total_Record_Count": 3,
"date": "2020-08-03T00:00:00Z"
},
{
"Total_Record_Count": 18,
"date": "2020-07-01T00:00:00Z"
},
{
"Total_Record_Count": 18754,
"date": "2020-06-16T00:00:00Z"
},
{
"Total_Record_Count": 4451898,
"date": "2020-06-08T00:00:00Z"
}
]})
| project Value = d.['value']
| mv-expand Value
| project Count = tolong(Value.['Total_Record_Count']), Date = todatetime(Value.['date'])
Count
Date
4451898
2020-06-08 00:00:00.0000000
18754
2020-06-16 00:00:00.0000000
18
2020-07-01 00:00:00.0000000
3
2020-08-03 00:00:00.0000000
1
2020-09-01 00:00:00.0000000
6
2020-10-01 00:00:00.0000000
4
2020-11-02 00:00:00.0000000
521
2020-12-01 00:00:00.0000000
27345
2020-12-16 00:00:00.0000000
134516
2020-12-18 00:00:00.0000000
26964
2021-01-15 00:00:00.0000000
6
2021-02-01 00:00:00.0000000
27254
2021-02-24 00:00:00.0000000
16608
2021-03-01 00:00:00.0000000

RamdaJS groupBy and tranform object

I want to transform this array of objects using RamdaJS. From this array of objects
let children = [
{ "name": "Bob", "age": 8, "father": "Mike" },
{ "name": "David", "age": 10, "father": "Mike" },
{ "name": "Amy", "age": 2, "father": "Mike" },
{ "name": "Jeff", "age": 11, "father": "Jack" }
]
into this array of objects
let fatherAndKids = [
{
"father": "Mike",
"count" : 3,
"kids": [
{ "name": "Bob", "age": 8 },
{ "name": "David", "age": 10 },
{ "name": "Amy", "age": 2
}
]
},
{
"father": "Jack",
"count" : 1,
"kids": [
{ "name": "Jeff", "age": 11 }
]
}
]
Here's what i did so far. But i failed to remove the father keys from kids's array
R.pipe(
R.groupBy(R.prop('father')),
R.map(kids => ({
father: R.head(kids)["father"],
count: kids.length,
kids: kids
})),
R.values()
)(children)
Use R.applySpec to create the object, and use R.map with R.dissoc to remove the 'father' property:
const { pipe, groupBy, prop, applySpec, head, length, map, dissoc, values } = R
const fn = pipe(
groupBy(prop('father')),
map(applySpec({
father: pipe(head, prop('father')),
count: length,
kids: map(dissoc('father'))
})),
values
)
const children = [
{ "name": "Bob", "age": 8, "father": "Mike" },
{ "name": "David", "age": 10, "father": "Mike" },
{ "name": "Amy", "age": 2, "father": "Mike" },
{ "name": "Jeff", "age": 11, "father": "Jack" }
]
const result = fn(children)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

Mongo DB query: project array element to property

Having following data
{
"_id": "...",
"name": "John",
"purchases": [
{
"date": {
"$date": "2016-12-20T00:00:00.000Z"
},
"amount": 1000
},
{
"date": {
"$date": "2016-12-23T00:00:00.000Z"
},
"amount": 100
}
]
}
How to get latest purchase amount as result field with different name?
To get next result:
{
"_id": "...",
"name": "John",
"purchases": [
{
"date": {
"$date": "2016-12-20T00:00:00.000Z"
},
"amount": 1000
},
{
"date": {
"$date": "2016-12-23T00:00:00.000Z"
},
"amount": 100
}
]
},
"latestPurchaseAmount": 100
}
You have to use the $rename take a look at this link
And for your last item you should look at this it will helps you Link
You can do something like this. This below query will unwind all the purchases in the collection and, sort the purchases by date descending and, limit the result to one and project the corresponding amount.
db.purchases.aggregate([{
$unwind: "$purchases"
}, {
$sort: {
"purchases.date": -1
}
}, {
$limit: 1
}, {
$project: {
_id: 0,
latestPurchaseAmount: "$purchases.amount"
}
}]);
Output
{ "latestPurchaseAmount" : 100 }

Need to get the following query to output correctly

Hi guys I've been trying all day to construct this simple mongo query but I can't get the desire output. Below is the query and the current output.
db.customers.aggregate(
{ $match : { "status" : "Closed" } },
{ $unwind: "$lines" },
{ $group : {
_id:{label: "$lines.label",date: {$substr: [ "$date", 0, 10 ]}},
values: { $push: { date: {$substr: [ "$date", 0, 10 ]}} },
count: { $sum: 1 }
}},
{ $project : {
_id : 1,
values:1,
count:1
}}
);
Which outputs the following.
{
"result": [
{
"_id": {
"label": "label",
"date": "2010-10-01"
},
"values": [
{
"date": "2010-10-01"
},
{
"date": "2010-10-01"
},
{
"date": "2010-10-01"
},
{
"date": "2010-10-01"
}
],
"count": 4
},
{
"_id": {
"label": "label",
"date": "2010-10-10"
},
"values": [
{
"date": "2010-10-10"
}
],
"count": 1
},
{
"_id": {
"label": "label",
"date": "2010-07-25"
},
"values": [
{
"date": "2010-07-25"
}
],
"count": 1
}
]
}
However the output below is the one that I'm looking for and just can't get. I can obviously get all the data I desire, just in the wrong places.
{
"result": [
{
"_id": "label",
"values": [
{
"date": "2010-11-27",
"count": 4
},
{
"date": "2010-10-10",
"count": 1
},
{
"date": "2010-07-25",
"count": 1
}
]
}
]
}
Like always thanks for the help and support.
Can you try this:
db.customers.aggregate([
{ $match : { "status" : "Closed" } },
{ $unwind: "$lines" },
{ $group : {
_id:{label: "$lines.label",date: {$substr: [ "$date", 0, 10 ]}},
values: { $push: { date: {$substr: [ "$date", 0, 10 ]}} },
count: { $sum: 1 }
}},
// This one I added to group them behind a single label in an array list
{ $group : {
_id:{
label: "$_id.label"
},
values : {
$push : { date : "$date", count : "$count" }
}
}
},
{ $project : {
_id : 1,
values:1,
count:1
}
}
]);
If I got your problem right, you like to group the counts + dates in an values array. That you can do with $push after the 1st group stage.