Add element to elasticsearch array in node.js - node.js

Can someone help me add an element to my ElasticSearch array:
let color = "red"
client.update({
index,
type: "Cars",
id,
body: {
script: {
inline: "if(! ctx._source.colors.contains(color)){ ctx._source.colors += color }",
params: {
color
}
}
}
})
For some reason, I keep getting color is not defined..
Thanks in advance!

You have to reference the param with: params.color in your script:
inline: "if(! ctx._source.colors.contains(params.color)){ ctx._source.colors += params.color }"

Related

ChartJS v3 custom displayFormats

I'm trying to add a string (Week) next to the week number in xAxes in Chartjs 3. But it shows a random number instead of the string.
Here is the part of my code in the options:
scales: {
x: {
type: 'time',
time: {
unit: 'week',
displayFormats: {
week: "'Week' W",
},
tooltipFormat: 'YYYY-W',
},
ticks: {
source: 'data',
},
offset: true,
},
.........
Current output:
'51124' 10
Expected output:
Week 10
any help would be appreciated.
Thanks
"'Week' W" is a formating string, depending on which adapter you are using 'Week' will be translated to a specific value.
You could check if your adapter support custom strings in the format-string,...
Or you try to modify the label with a callback, like shown here in the documentation: https://www.chartjs.org/docs/latest/axes/labelling.html
Here the relevat code, adapted to fit your code snippet:
...
scales: {
x: {
type: 'time',
time: {
unit: 'week',
displayFormats: {
week: 'W',
}
},
ticks: {
// This prepends the "Week" before the label
callback: function(value, index, ticks) {
return '"Week" ' + value;
}
}
}
}
...

Fetch key value from json data using terraform

I have a json data file from which I need to fetch only the id attribute value into a separate list variable. I tried for loop but not able to get the required data using terraform. Can someone tell how to fetch the id part ? your help is much appreacited.
code:
locals {
data = jsondecode(file("./data.json"))[*]
sections = [ for item in local.data : item ]
}
output "ids" {
value = [for a in local.sections[0]: a]
}
json file:
{
"c":[
{
"id":"6",
"key":"c",
"name":"s01"
}
],
"l":{
"id":"7",
"key":"l",
"name":"s02"
},
"m":{
"id":"8",
"key":"mp",
"name":"s03"
},
"n":{
"id":"5",
"key":"cn",
"name":"s04"
},
"od":"odk",
"s":{
"id":"9",
"key":"cs",
"name":"s05"
},
"ss":{
"id":"1",
"key":"ss",
"name":"s06"
},
"in":{
"id":"65",
"key":"cn",
"name":"s07"
},
"b":{
"id":"2",
"key":"cb",
"name":"s08"
}
}
Not sure if "od":"odk", is mistake or not, but you can do the following:
locals {
data = jsondecode(file("./data.json"))
}
output "ids" {
value = [for v in values(local.data): v.id if can(v.id)]
}

Deleting items from the array of objects

I have a document that looks like:
{
_id: "....",
hostname: "mysite.com",
text: [
{
source: "this is text. this is some pattern",
...
},
{
source: "....",
...
}
]
}
and I am trying to delete the items from the text array which match a specific condition in my query given as:
db.getCollection('TM').updateMany(
{hostname: "mysite.com"},
{
$pull: {
"text.source": /this is some pattern/
}
},
{ multi: true }
)
Here I want to delete all the items from the array where the value inside source matches this is some pattern. When I execute this query, it gives an error saying: Cannot use the part (source) of (text.source) to traverse the element with error code 28.
What is the way to achieve this?
it gives an error saying: Cannot use the part (source) of (text.source) to traverse the element with error code 28.
Incorrect syntax of $pull for update methods,
Corrected syntax, and You can use $regex to find document by specific pattern,
"text.source" the condition in filter part will filter main documents, it is optional
text: { source: will filter sub documents and pull elements
db.getCollection('TM').updateMany(
{
hostname: "mysite.com",
"text.source": { $regex: "this is some pattern" }
},
{
$pull: {
text: { source: { $regex: "this is some pattern" } }
}
}
)
Playground

JointJS: Add a label on the validateConnection Event?

I have a validateConnection event within which I have a lot of conditions. Now on one of these conditions, I want to add a custom label to the link that gets created. How do I do this within validateConnection
You can try this:
prepare a 'placeholder' for the future label - it creates a label without text:
new joint.dia.Link({
labels: [
{ position: 0.5 }
]
}),
Then in the validateConnection set the label text value throught the attr
validateConnection: function(cellViewS, magnetS, cellViewT, magnetT, end, linkView) {
if (cellViewT) {
linkView.model.prop('labels/0/attrs/text/text', cellViewT.model.attr('text/text'));
} else {
linkView.model.prop('labels/0/attrs/text/text', '')
}
}
https://jsfiddle.net/vtalas/hxbfo0m4/
joint.dia.Link({
labels: [
{ position: 0.5, attrs: { text: { text: 'test' } } }
]

knockout foreach computed value

I am trying to add a computed value to my viewModel object. And I am using foreach to create a table of rows. I am not able to get around this computed function.
I am trying to do this.
viewModel =
{
objectName: ko.observable([
{ value: "", triggerValue: "0"},
{ value: "", triggerValue: "1"},
{ value: "", triggerValue: "1"}
]),
};
viewModel.objectName().value= ko.computed(function() {
return this.objectName().triggerValue= "0" ? "Apple" : "Microsoft";
}, this);
I want the viewModel objectName output to look like
{value: "Apple", triggerValue: "0"},
{value: "Microsoft", triggerValue: "1"},
{value: "Microsoft", triggerValue: "1"}
Thanks.
KDK
Several mistakes going on here
you are using an observable instead of observableArray, technically an observable can store an array, but you are better off using an observableArray
you are trying to tie a computed to objectName().value, but object name is suppose to be an array so it would not have a value, it would ideally be like this objectName()[1].value.
This is not how to assign properties, ko.computed is not a replacement for a function, computed are for monitoring existing observales and and re calculating when a change is made in one of the monitored observables.
I would do something like this.
viewModel =
{
objectName: ko.observable([
{ value: setType(0), triggerValue: "0"},
{ value: setType(1), triggerValue: "1"},
{ value: setType(1), triggerValue: "1"}
]),
};
function setType(trigger){
return trigger = "0" ? "Apple" : "Microsoft"
}
or better yet
viewModel =
{
objectName: ko.observable([
setVal(0),
setVal(1),
setVal(1),
]),
};
function setVal(trigger){
return {value: (trigger = "0" ? "Apple" : "Microsoft"), triggerValue: trigger };
}

Resources