someone colud please help me with this situation?
I have this fake JSON...
[
{
"user": {
"type": "PF",
"code": 12345,
"Name": "Darth Vader",
"currency": "BRL",
"status": "SINGLE",
"localization": "NABOO",
"createDate": 1627990848665,
"olderAdress": [
{
"localization": "DEATH STAR",
"createDate": 1627990848775
},
{
"localization": "TATOOINE",
"createDate": 1627990555888
},
]
}
}
]
My idea is, i need to extract the "olderAdress" and create new register but I need to keep the original register too.
Example: This is the result I hope.
[
{
"_id": ObjectId("5a3456e000102030405000000"),
"user": {
"Name": "Darth Vader",
"code": 12345,
"createDate":1627990848665,
"currency": "BRL",
"localization": "NABOO",
"status": "SINGLE",
"type": "PF"
}
},
{
"_id": ObjectId("5a789e000102030405000000"),
"user": {
"Name": "Darth Vader",
"code": 12345,
"createDate": 1627990848775,
"currency": "BRL",
"localization": "DEATH STAR",
"status": "SINGLE",
"type": "PF"
}
},
{
"_id": ObjectId("5a991e000102030405000000"),
"user": {
"Name": "Darth Vader",
"code": 12345,
"createDate": 1627990555888,
"currency": "BRL",
"localization": "TATOOINE",
"status": "SINGLE",
"type": "PF"
}
}
]
I try same think in this link (Test to extract values) to tests but unfortunately I cant. Somewoone could please help me?
You're very close, what you want to do is just to add the new location to the array before $unwinding it.
like so:
db.collection.aggregate([
{
"$addFields": {
"user.olderAdress": {
"$concatArrays": [
"$user.olderAdress",
[
{
"localization": "NABOO",
"createDate": "$$NOW"
}
]
]
}
}
},
... rest of pipeline ...
])
Mongo Playground
Related
I have a lot of data I need to parse though.
I need to pull all pid's and price's.
`
[
{
"id": 159817,
"price": "10.69",
"stocked": true,
"store": {
"id": 809,
"nsn": "22036-0",
"pricingSource": "manual",
"lastUpdated": "2022-12-05T15:24:33.908Z"
},
"sharedFields": {
"type": "PRODUCT",
"id": 24549,
"pid": "12079",
"labels": [
{
"type": "default",
"value": "Chicken Sandwich",
"locale": "en"
},
{
"type": "fresh",
"value": "Chicken",
"locale": "en"
},
{
"type": "product_json",
"value": "Chicken",
"locale": "en"
}
],
"calMin": 600,
"calMax": 600,
"lastUpdated": "2021-12-31T13:49:22.794Z"
}
},
{
"id": 159818,
"price": "9.29",
"stocked": true,
"store": {
"id": 809,
"nsn": "22036-0",
"pricingSource": "manual",
"lastUpdated": "2022-12-05T15:24:33.908Z"
},
"sharedFields": {
"type": "PRODUCT",
"id": 25,
"pid": "1",
"labels": [
{
"type": "default",
"value": "Ham Sandwich",
"locale": "en"
},
{
"type": "fresh",
"value": "Ham",
"locale": "en"
}
],
"calMin": 540,
"calMax": 540,
"lastUpdated": "2021-07-09T19:30:00.326Z"
}
}
]
`
and I need to place them into a string like this, but on a scale of 150 products. I'd also need to change "pid" to "productId"
[{ "productId": "46238", "price": 6.09 }, { "productId": "40240", "price": 1.49 }]
I need to add a string before this data, but I'm pretty confident I can figure that part out.
I am pretty open to the easiest suggestion, whether that be VBS, Excel macro, etc.
Now I have a new situation Version 3.0.
I have this fake json:
[
{
"type":"PF",
"code":12345,
"Name":"Darth Vader",
"currency":"BRL",
"status":"ACTIVE",
"localization":"NABOO",
"createDate":1627990848665,
"olderAdress":[
{
"localization":"DEATH STAR",
"status":"BLOCKED",
"createDate":1627990848665
},
{
"localization":"TATOOINE",
"status":"CANCELLED",
"createDate":1627990555665
},
{
"localization":"ALDERAAN",
"status":"INACTIVED",
"createDate":1627990555665
}
]
},
{
"type":"PF",
"code":12345,
"Name":"Anakin Skywalker",
"currency":"BRL",
"status":"ACTIVE",
"localization":"NABOO",
"createDate":1627990848665,
"olderAdress":null
}
]
And I need to add a new field in each array element ONLY IF THE ARRAY IS NOT NULL. But I need to do that by aggregate because I'm using this result in Spring before sending to the users.
I need this result:
[
{
"type": "PF",
"code": 12345,
"Name": "Darth Vader",
"currency": "BRL",
"status": "ACTIVE",
"localization": "NABOO",
"createDate": 1627990848665,
"olderAddress": [
{
"localization": "DEATH STAR",
"status": "BLOCKED",
"createDate": 1627990848665,
"isItemOfOlderAddress" : true
},
{
"localization": "TATOOINE",
"status": "CANCELLED",
"createDate": 1627990555665,
"isItemOfOlderAddress" : true
},
{
"localization": "ALDERAAN",
"status": "INACTIVED",
"createDate": 1627990555665,
"isItemOfOlderAddress" : true
},
]
},
{
"type": "PF",
"code": 12345,
"Name": "Anakin Skywalker",
"currency": "BRL",
"status": "ACTIVE",
"localization": "NABOO",
"createDate": 1627990848665,
"olderAdress": null
},
]
So I added the field isItemOfOlderAddress only where olderAddress is not null and where olderAddress is null I only show the default information. How can I do that?
Query
if olderAdress is an array(so not null also), add "isItemOfOlderAddress": true field to all members
else keep the old value(so keep the null also)
Test code here
db.collection.aggregate([
{
"$set": {
"olderAdress": {
"$cond": [
{
"$isArray": [
"$olderAdress"
]
},
{
"$map": {
"input": "$olderAdress",
"in": {
"$mergeObjects": [
"$$this",
{
"isItemOfOlderAddress": true
}
]
}
}
},
"$olderAdress"
]
}
}
}
])
I have the following JSON which have an element array_within_array in another array. So I want to pull both array_within_array element and upper array element(event_array) to root level using jq.
So here there are 3 events.
meal_selection
login
placed_order
Event placed_order have two sub-events in array. So after conversion there should be 4 events(1 from meal_selection, 1 from login and 2 from placed_order). these all should be on the same level.
Here is the JSON
{
"region": "USA",
"user_id": "123",
"event_array": [{
"event_attributes": {
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
"event_name": "meal_selection",
"created_at": "2021-08-13 01:28:57"
},
{
"event_name": "login",
"created_at": "2021-08-13 01:29:02"
},
{
"event_attributes": {
"array_within_array": [
{
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
{
"date": "2021-08-18",
"category": "dinner",
"location": "home"
}
]
},
"event_name": "placed_order",
"created_at": "2021-08-13 01:28:08"
}
]
}
and I want to convert to the below one
{
"region": "USA",
"user_id": "123",
"event_attributes": {
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
"event_name": "meal_selection",
"created_at": "2021-08-13 01:28:57"
}
{
"region": "USA",
"user_id": "123",
"event_name": "login",
"created_at": "2021-08-13 01:29:02"
}
{
"region": "USA",
"user_id": "123",
"event_attributes": {
"date": "2021-08-17",
"category": "lunch",
"location": "office"
},
"event_name": "placed_order",
"created_at": "2021-08-13 01:28:08"
}
{
"region": "USA",
"user_id": "123",
"event_attributes": {
"date": "2021-08-18",
"category": "dinner",
"location": "home"
},
"event_name": "placed_order",
"created_at": "2021-08-13 01:28:08"
}
Here's a straightforward solution that keeps things simple by using two steps:
{ region, user_id} + (.event_array[] )
| if .event_attributes|has("array_within_array")
then .event_attributes.array_within_array as $a
| .event_attributes = $a[]
else .
end
I'm unable to execute payment transaction in Stellar testnet.
Initially i use find_path:
curl -X POST https://test.stellar.org:9002 -d '
{
"method": "static_path_find",
"params": [
{
"source_account": "g4e5v2ERpKvdBZrchn6DrWUtvezkXsu5wo",
"destination_account": "gJ3e65GzqERgeeS5oXsv8NGfdZWzm93ej6",
"source_currencies" : [ { "currency" : "SGD", "issuer" : "gDfapfG5hDHuYkbVpugtupkGYVTKXwd59r"} ],
"destination_amount": {
"currency": "MYR",
"value": "1",
"issuer": "gsQuUKcxM68nEX9tKR71UH5hZXwrvT2mpS"
}
}
]
}'
The result I get:
{
"result": {
"alternatives": [
{
"paths_computed": [
[
{
"currency": "MYR",
"issuer": "gsQuUKcxM68nEX9tKR71UH5hZXwrvT2mpS",
"type": 48
},
{
"account": "gsQuUKcxM68nEX9tKR71UH5hZXwrvT2mpS",
"type": 1
}
]
],
"source_amount": {
"currency": "SGD",
"issuer": "gDfapfG5hDHuYkbVpugtupkGYVTKXwd59r",
"value": "0.375"
}
}
],
"destination_account": "gJ3e65GzqERgeeS5oXsv8NGfdZWzm93ej6",
"destination_currencies": [
"MYR",
"STR"
],
"status": "success"
}
}
Then I trying to broadcast transaction in the network:
curl -X POST https://test.stellar.org:9002 -d '
{
"method": "submit",
"params": [
{
"secret": "sfk7dUd4N8cxVFVowsZL4DQgonNP9k8WFBgaV2NmwEQKGzWuqXT",
"tx_json": {
"TransactionType": "Payment",
"Account": "g4e5v2ERpKvdBZrchn6DrWUtvezkXsu5wo",
"Destination": "gJ3e65GzqERgeeS5oXsv8NGfdZWzm93ej6",
"Amount": {
"currency": "MYR",
"value": "1",
"issuer": "gsQuUKcxM68nEX9tKR71UH5hZXwrvT2mpS"
},
"Paths": [
[
{
"currency": "MYR",
"issuer": "gsQuUKcxM68nEX9tKR71UH5hZXwrvT2mpS",
"type": 48
},
{
"account": "gsQuUKcxM68nEX9tKR71UH5hZXwrvT2mpS",
"type": 1
}
]
]
}
}
]
}'
And finally I'm getting unsuccessful result when transaction passed through consensus:
{
"engine_result": "tecPATH_PARTIAL",
"engine_result_code": 101,
"engine_result_message": "Path could not send full amount.",
"ledger_hash": "82BCF42941EF00BEA9E575B696CD88EC7E14FDE8CFD39FDA6A76B5710FDD0755",
"ledger_index": 704612,
"meta": {
"AffectedNodes": [
{
"ModifiedNode": {
"FinalFields": {
"Account": "g4e5v2ERpKvdBZrchn6DrWUtvezkXsu5wo",
"Balance": "199999960",
"Flags": 0,
"OwnerCount": 1,
"Sequence": 5
},
"LedgerEntryType": "AccountRoot",
"LedgerIndex": "D3433DEE556200D8AB72DED7156A597A273103D2BC50F526193EB9C4D8C8B735",
"PreviousFields": {
"Balance": "199999970",
"Sequence": 4
},
"PreviousTxnID": "6ADF48B45D95B3ABD20FD78F95F7E4F20D91FF5D169ECC5EFD6DB0CA78BEA486",
"PreviousTxnLgrSeq": 704445
}
}
],
"TransactionIndex": 0,
"TransactionResult": "tecPATH_PARTIAL"
},
"status": "closed",
"transaction": {
"Account": "g4e5v2ERpKvdBZrchn6DrWUtvezkXsu5wo",
"Amount": {
"currency": "MYR",
"issuer": "gsQuUKcxM68nEX9tKR71UH5hZXwrvT2mpS",
"value": "1"
},
"Destination": "gJ3e65GzqERgeeS5oXsv8NGfdZWzm93ej6",
"Fee": "10",
"Flags": 2147483648,
"Paths": [
[
{
"currency": "MYR",
"issuer": "gsQuUKcxM68nEX9tKR71UH5hZXwrvT2mpS",
"type": 48
},
{
"account": "gsQuUKcxM68nEX9tKR71UH5hZXwrvT2mpS",
"type": 1
}
]
],
"Sequence": 4,
"SigningPubKey": "4B640AC0C64AEF1C97EAAD0629CADDCA95FBA5F095BD20BD6282BEC450A125B1",
"TransactionType": "Payment",
"TxnSignature": "7BF5772EB916F20F56C5F148E99C3C3DD77C2916EAA47AB2D7FA8306F2B4A9D3FA4B789BDB79899A634B0C33F50A6ACB447CA6A9633C1C73BAB754FBFE74C105",
"date": 485412700,
"hash": "846AB6502D3B7A3137B2B538E6616B9C4459D2A67C48E58AF6B64F4659FF0016"
},
"type": "transaction",
"validated": true
}
Why? What I did wrong? I really can't understand...
Anyway I found the problem.
I forgot to add required SendMax parameter when submitting transaction.
curl -X POST https://test.stellar.org:9002 -d '
{
"method": "submit",
"params": [
{
"secret": "sfk7dUd4N8cxVFVowsZL4DQgonNP9k8WFBgaV2NmwEQKGzWuqXT",
"tx_json": {
"TransactionType": "Payment",
"Account": "g4e5v2ERpKvdBZrchn6DrWUtvezkXsu5wo",
"Destination": "gJ3e65GzqERgeeS5oXsv8NGfdZWzm93ej6",
"Amount": {
"currency": "MYR",
"value": "1",
"issuer": "gsQuUKcxM68nEX9tKR71UH5hZXwrvT2mpS"
},
"Paths": [
[
{
"currency": "MYR",
"issuer": "gsQuUKcxM68nEX9tKR71UH5hZXwrvT2mpS",
"type": 48
},
{
"account": "gsQuUKcxM68nEX9tKR71UH5hZXwrvT2mpS",
"type": 1
}
]
],
"SendMax" : { "currency" : "SGD", "value" : "0.375", "issuer": "gDfapfG5hDHuYkbVpugtupkGYVTKXwd59r" }
}
}
]
}'
I am a noob when it comes to json. The more I use it, I am starting to like it. I have a output that looks like this
[
{
"product": {
"id": "6",
"category": "Books",
"created": "2010-04-13 15:15:18",
},
"availability": [
{
"country": "United Kingdom",
"price": "$13.99",
},
{
"country": "Germany",
"price": "$13.99",
}
]
}
]
Actually I have listed only one product, But output has many products listed. I want to loop through this json output and get all the product's category, countries where they are available and price using fbjs.
I appreciate any help.
Thanks.
If your JSON is like this,
var products = [
{
"product": {
"id": "6",
"category": "Books",
"created": "2010-04-13 15:15:18",
},
"availability": [
{
"country": "United Kingdom",
"price": "$13.99",
},
{
"country": "Germany",
"price": "$13.99",
}
]
},
{
"product": {
"id": "7",
"category": "Books",
"created": "2010-04-13 15:15:18",
},
"availability": [
{
"country": "United Kingdom",
"price": "$13.99",
},
{
"country": "Germany",
"price": "$13.99",
}
]
}
]
You can iterate through:
for(var index = 0; index < products.length; index++) {
alert(products[index].product.category);
}