Merging two JSON files while summing up values - mule-studio

I get two different files with stock information which I would like to join into one file while summing the data up, based on "pid".
payloadA
{
"order": [
{
"pid": "a",
"instock": "2"
},
{
"pid": "b",
"instock": "2"
},
{
"pid": "c",
"instock": "2"
}
]
}
payloadB
{
"order": [
{
"pid": "c",
"instock": "2"
},
{
"pid": "d",
"instock": "2"
}
]
}
Result
payloadA
{
"order": [
{
"pid": "a",
"instock": "2"
},
{
"pid": "b",
"instock": "2"
},
{
"pid": "c",
"instock": "4"
},
{
"pid": "c",
"instock": "2"
}
]
}
But what I'm struggeling with are the edge cases:
pids might be in both or as well in only one of the datasets
payloadB might be empty
{
"order": []
}

You can try the following DataWeave expression:
%dw 2.0
output application/json
import * from dw::core::Arrays
var payloadA = {
"order": [
{
"pid": "a",
"instock": "2"
},
{
"pid": "b",
"instock": "2"
},
{
"pid": "c",
"instock": "2"
}
]
}
var payloadB = {
"order": [
{
"pid": "c",
"instock": "2"
},
{
"pid": "d",
"instock": "2"
}
]
}
---
"order": outerJoin(payloadA.order, payloadB.order, (itemA) -> itemA.pid, (itemB) -> itemB.pid) map (item, index) -> {
"pid": item.l.pid default item.r.pid,
"instock": (item.l.instock default 0) + (item.r.instock default 0)
}
Output:
{
"order": [
{
"pid": "a",
"instock": 2
},
{
"pid": "b",
"instock": 2
},
{
"pid": "c",
"instock": 4
},
{
"pid": "d",
"instock": 2
}
]
}

Related

MongoDb : query to match in same embedded document

Help me to match in single embedded document of both conditions.
db.inventory.insertOne([
... { "item": "journal", "instock": [ { "warehouse": "A", "qty": 5 }, { "warehouse": "C", "qty": 15 } ] },
... { "item": "notebook", "instock": [ { "warehouse": "C", "qty": 5 } ] },
... { "item": "paper", "instock": [ { "warehouse": "A", "qty": 60 }, { "warehouse": "B", "qty": 15 } ] },
... { "item": "planner", "instock": [ { "warehouse": "A", "qty": 40 }, { "warehouse": "B", "qty": 5 } ] },
... { "item": "postcard", "instock": [ { "warehouse": "B","qty": 15 }, { "warehouse": "C", "qty": 35 } ] }
... ])
Expected to return single document which match exactly as queried but returns two.
db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } )
{ "_id" : ObjectId("63061a1bb87c1278047a2717"), "item" : "journal", "instock" : [ { "warehouse" : "A", "qty" : 5 }, { "warehouse" : "C", "qty" : 15 } ] }
{ "_id" : ObjectId("63061a1bb87c1278047a271a"), "item" : "planner", "instock" : [ { "warehouse" : "A", "qty" : 40 }, { "warehouse" : "B", "qty" : 5 } ] }
You need to perform the following query:
db.inventory.find({
instock: { $elemMatch: { qty: 5, warehouse: 'A' } }
});
Documentation for reference: https://www.mongodb.com/docs/v5.0/tutorial/query-array-of-documents/#a-single-nested-document-meets-multiple-query-conditions-on-nested-fields

Mongodb: How to merge $facet output 2 by 2?

Playground:https://mongoplayground.net/p/YUV_fReyGsr
I have following query. I need to combine the result 2 by 2. Meaning I need to combine facet "1","2" as a result and facet "3","4" as another result. It's guaranteed that the number of facet will be even. Also, each pair of facet should get at most one record(it might not matter)
db.collection.aggregate([
{
"$facet": {
"1": [
{
$match: {
"ID": "2"
}
}
],
"2": [
{
$match: {
"array.ID": "2"
}
}
],
"3": [
{
$match: {
"array.ID": "4"
}
}
],
"4": [
{
$match: {
"ID": "4"
}
}
]
}
}
])
The expected result will be
[
{
"1": [
{
"ID": "1",
"array": [
{
"ID": "2",
"attribute1": "456"
},
{
"ID": "3",
"attribute1": "567"
}
],
"attr1": "123"
}
],
"2": [
{
"ID": "4",
"array": [
{
"ID": "5",
"attr1": "456"
}
],
"attr1": "123"
}
]
}
]
I was able to figure this out using $concatArrays operator, along with $project.
Live demo here
Database
[
{
"ID": "1",
"attr1": "123",
"array": [
{
"ID": "2",
"attribute1": "456"
},
{
"ID": "3",
"attribute1": "567"
}
]
},
{
"ID": "4",
"attr1": "123",
"array": [
{
"ID": "5",
"attr1": "456"
}
]
}
]
Query
db.collection.aggregate([
{
"$facet": {
"1": [
{
$match: {
"ID": "2"
}
}
],
"2": [
{
$match: {
"array.ID": "2"
}
}
],
"3": [
{
$match: {
"array.ID": "4"
}
}
],
"4": [
{
$match: {
"ID": "4"
}
}
]
}
},
{
"$project": {
_id: 0,
"1": {
"$concatArrays": [
"$1",
"$2"
]
},
"2": {
"$concatArrays": [
"$3",
"$4"
]
}
}
}
])
Result
[
{
"1": [
{
"ID": "1",
"_id": ObjectId("5a934e000102030405000000"),
"array": [
{
"ID": "2",
"attribute1": "456"
},
{
"ID": "3",
"attribute1": "567"
}
],
"attr1": "123"
}
],
"2": [
{
"ID": "4",
"_id": ObjectId("5a934e000102030405000001"),
"array": [
{
"ID": "5",
"attr1": "456"
}
],
"attr1": "123"
}
]
}
]

MongoDB distinct returns empty

I can't get collection.distinct to work for me on db.version '4.4.3'. I'm following the example at https://docs.mongodb.com/manual/reference/command/distinct/
{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] }
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] }
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" }
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
Then run
db.runCommand ( { distinct: "inventory", key: "dept" } )
I get
{ values: [], ok: 1 }
This doesn't work either:
db.inventory.distinct( "dept" )
[]
I can't get it to work on a local server or a remote server. What gives?

MongoDB query with and in the same object array

I have a JSON file like this:
{
"level1": [
{},
{},
{
"level2": {
"level3": [
[
{
"second": 0
}
],
[
{
"second": 1,
"label": "A",
"frame": 40
}
],
[
{
"second": 2,
"label": "A",
"frame": 60
},
{
"second": 2,
"label": "B",
"frame": 60
}
],
[
{
"second": 3,
"label": "B",
"frame": 90
}
]
]
}
}
]
}
I would like to find the elements A and B when they occur contemporarily in the same second. I'm able to find label "A" or label "B" but I would like that the result of the query is second 2 or a list of all seconds when both elements appear.

Extracting unique elements from array within different arrays

I have a collection with this structure:
{
"name": "1",
"array1": [
{
"arrayname1A" : "A",
"array2": [
{ "value": "1" },
{ "value": "3" }
]
},
{
"arrayname1B": "B",
"array2": [
{ "value": "5" },
]
}
]
},
{
"name": "2",
"array1": [
{
"arrayname1A": "A",
"array2": [
{ "value": "1" },
{ "value": "7" }
]
}
]
}
How can I extract the unique "value" from every different array2? The end result I am looking for would be something like "1","3","5","7" with no repeated values.
Simply use the distinct() method and the dot notation to access the "value" field.
db.collection.distinct('array1.array2.value')
which yields:
[ "1", "3", "5", "7" ]