I'm working on a module and I have a variable structure like the following:
input = [
{
"prod_db" : {...someOtherAttributes}
"prod_app": {...someOtherAttributes}
},
{
"stage_db" : {...someOtherAttributes}
"stage_app": {...someOtherAttributes}
},
{
"dev_db" : {...someOtherAttributes}
"dev_app": {...someOtherAttributes}
}
]
is there a way to have a map of only the second-level object? something like this:
result = {
"prod_db" : {...someOtherAttributes},
"prod_app": {...someOtherAttributes},
"stage_db" : {...someOtherAttributes},
"stage_app": {...someOtherAttributes},
"dev_db" : {...someOtherAttributes},
"dev_app": {...someOtherAttributes}
}
Your expected result is invalid data structure. I guess that you actually want the following:
result = merge(var.input...)
Where ... is for Expanding Function Arguments.
Related
I have a JSON file with the following and trying to list the shapes ex: "t3-nano, t3-micro, t3-small, t2.medium, t3-2xlarge, r6g-medium".
json file = info.json
{
"t3-nano" : {
"service_name" : "t3",
"existing" : 100
},
"t3-micro" : {
"service_name" : "t3",
"existing" : 1
},
"t3-small" : {
"service_name" : "t3",
"existing" : 2
},
"t2.medium" : {
"service_name" : "t2",
"existing" : 0
},
"t3-2xlarge" : {
"service_name" : "t3-2",
"existing" : 5
},
"r6g-medium" : {
"service_name" : "r6g.medium",
"existing" : 10
}
}
I tried the following
locals {
service_name = flatten([for i in local.info : i[*].service_name])
shapes = flatten([for i in local.info : i[*].index])
}
and it got failed
Error: Unsupported attribute
This object does not have an attribute named "index".
I was expecting to print shapes = [t3-nano, t3-micro, t3-small, t2.medium, t3-2xlarge, r6g-medium]. Can someone help if there is a way to just list the shapes?
The flatten function and for expression are both unnecessary here. The function keys already has the functionality and return value that you want to achieve:
shapes = keys(local.info)
and that will assign the requested value.
Is it possible with HCL to have nested iterations returning a flat list(map) without resorting to flatten?
I have this:
locals {
mappings = flatten([
for record_type in var.record_types : [
for host in var.hosts : {
type = record_type,
host = host
}
]
])
}
I would like to remove the need for flatten like this:
locals {
mappings = [
for record_type in var.record_types :
for host in var.hosts : {
type = record_type,
host = host
}
]
}
But it seems like each for .. in must return data.
One alternative I could think of to only have a single for-loop is using setproduct():
variable "record_types" {
default = ["type1", "type2"]
}
variable "hosts" {
default = ["host1", "host2"]
}
locals {
mappings = [
for i in setproduct(var.record_types, var.hosts) : {
type = i[0],
host = i[1],
}
]
}
output "mappings" {
value = local.mappings
}
after terraform apply resulting in:
Outputs:
mappings = [
{
"host" = "host1"
"type" = "type1"
},
{
"host" = "host2"
"type" = "type1"
},
{
"host" = "host1"
"type" = "type2"
},
{
"host" = "host2"
"type" = "type2"
},
]
Of course, the two variables need to be independent sets here.
If you want to support duplicates or have dependent inputs, flatten() with two loops is the way.
{
TypeList" : [
{
"TypeName" : "Carrier"
},
{
"TypeName" : "Not a Channel Member"
},
{
"TypeName" : "Service Provider"
}
]
}
Question :
db.supplies.find("text", {search:"\"chann\" \"mem\""})
For above query I want display :
{
TypeName" : "Not a Channel Member"
}
But I am unable to get my result.
What are changes I have to do in query .
Please help me.
The below query will return your desired result.
db.supplies.aggregate([
{$unwind:"$TypeList"},
{$match:{"TypeList.TypeName":{$regex:/.*chann.*mem.*/,$options:"i"}}},
{$project:{_id:0, TypeName:"$TypeList.TypeName"}}
])
If you can accept to get an output like this:
{
"TypeList" : [
{
"TypeName" : "Not a Channel Member"
}
]
}
then you can get around using the aggregation framework which generally helps performance by running the following query:
db.supplies.find(
{
"TypeList.TypeName": /chann.*mem/i
},
{ // project the list in the following way
"_id": 0, // do not include the "_id" field in the output
"TypeList": { // only include the items from the TypeList array...
$elemMatch: { //... where
"TypeName": /chann.*mem/i // the "TypeName" field matches the regular expression
}
}
})
Also see this link: Retrieve only the queried element in an object array in MongoDB collection
my schema looks like
{
qty:{
property1:{
//something
}
property2:[{
size:40,
color:"black",
enabled:"true"
}]
}
}
property 2 is array what i want to do is update those array object whose enabled is true in single query
I tried writing the following query
db.col.update({
"qty.property2.enabled" = "true"
}, {
"qty.property2.color" = "green"
}, callback)
but it is not working
error:
[main] Error: can't have . in field names [qty.pro.size]
db.col.update({"qty.property2.enabled":"true"},{$set: {'qty.property2.$.color': 'green'}}, {multi: true})
this is the way to update element inside array.
equal sign '=' cannot be used inside object
updating array is done using $
Alternative solution for multiple conditions:
db.foo.update({
_id:"i1",
replies: { $elemMatch:{
_id: "s2",
update_password: "abc"
}}
},
{
"$set" : {"replies.$.text" : "blah"}
}
);
Why
So I was looking for similar solution as this question, but in my case I needed array element to match multiple conditions and using currently provided answers resulted in changes to wrong fields.
If you need to match multiple fields, for example let say we have element like this:
{
"_id" : ObjectId("i1"),
"replies": [
{
"_id" : ObjectId("s1"),
"update_password": "abc",
"text": "some stuff"
},
{
"_id" : ObjectId("s2"),
"update_password": "abc",
"text": "some stuff"
}
]
}
Trying to do update by
db.foo.update({
_id:"i1",
"replies._id":"s2",
"replies.update_password": "abc"
},
{
"$set" : {"replies.$.text" : "blah"}
}
);
Would result in updating to field that only matches one condition, for example it would update s1 because it matches update_password condition, which is clearly wrong. I might have did something wrong, but $elemMatch solution solved any problems like that.
Suppose your documet looks like this.
{
"_id" : ObjectId("4f9808648859c65d"),
"array" : [
{"text" : "foo", "value" : 11},
{"text" : "foo", "value" : 22},
{"text" : "foobar", "value" : 33}
]
}
then your query will be
db.foo.update({"array.value" : 22}, {"$set" : {"array.$.text" : "blah"}})
where first curly brackets represents query criteria and second one sets the new value.
I am trying to do one of the most simple operations "Update" in a dynamo db list.
Table Schema -
businessId : String, customers: StringSet, itemCode : NumberSet
I have an entry inserted via put -
bussinessId = "sampleBusiness", cuatomers 0: "cust1", itemCode 0: 4554
I want to add more items using update and here is what I have tried -
var updateRequest = {
'TableName' : tableName,
'Key' : {
'businessId' : {
"S" : businessId
}
},
'UpdateExpression' : "SET itemCode[2] =:attrValue",
'ExpressionAttributeValues' : {
':attrValue' : {
"N" : "564564"
}
}
};
This gives me error -
Document Path provided in document is invalid
I wanted to append new entries so tried this as well -
var sm = [];
sm[0] = "56465";
//Add business to
var updateRequest = {
'TableName' : tableName,
'Key' : {
'businessId' : {
"S" : businessId
}
},
'UpdateExpression' : 'SET #attrName = list_append(#attrName, :attrValue)',
'ExpressionAttributeNames' : {
'#attrName' : 'itemCode'
},
'ExpressionAttributeValues' : {
':attrValue' : {
"NS" : sm
}
}
};
This gives:
ValidationException: Invalid UpdateExpression: Incorrect operand type for operator or function; operator or function: list_append, operand type: NS
Also attempted this -
':attrValue' : {
"N" : "4564"
}
But same error.
As per the example provided in http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Modifying.html , it adds a new element to the FiveStar review list. The expression attribute name #pr is ProductReviews; the attribute value :r is a one-element list. If the list previously had two elements, [0] and [1], then the new element will be [2].
SET #pr.FiveStar = list_append(#pr.FiveStar, :r)
which Says :r is one element list
I am missing some thing here. Request if any one can help. Struck on this for long time. I just want to append elements in set in dynamo db using nodeJS.
It looks like this:
'ExpressionAttributeValues' : {
':attrValue' : {
"NS" : sm
}
}
Should be this:
'ExpressionAttributeValues' : {
':attrValue' : {
"S" : sm
}
}
Or you need to cast this value sm[0] = "56465"; to a Number Number("56465") and change the :attrValue data type "S" to "N". Depends on how you have your table configured.
It's possible too that you should assign :attrValue to be "S" : sm[0] because right now you are passing an "S" a whole array.
I got a proper solution
var item = {"endTime": "7pm", "imageName": "7abcd", "startTime": "7pm"};
dynamo.updateItem({
TableName:'TableName',
Key:{"BucketName":"abcdefg" },
UpdateExpression : "SET #attrName = list_append(#attrName, :attrValue)",
ExpressionAttributeNames : {
"#attrName" : "ImageLists"
},
ExpressionAttributeValues : {
':attrValue' : [item]
}
},function(err, data) {
if (err)
console.log(err);
else
console.log(data)
});