Related
How can I filter if all num of qty greater then 50 then this is return otherwise not:
this is examples documents;
{
_id: ObjectId("5234cc89687ea597eabee675"),
code: "xyz",
tags: [ "school", "book", "bag", "headphone", "appliance" ],
qty: [
{ size: "S", num: 10, color: "blue" },
{ size: "M", num: 45, color: "blue" },
{ size: "L", num: 100, color: "green" }
]
}
{
_id: ObjectId("5234cc8a687ea597eabee676"),
code: "abc",
tags: [ "appliance", "school", "book" ],
qty: [
{ size: "6", num: 100, color: "green" },
{ size: "6", num: 70, color: "blue" },
{ size: "8", num: 100, color: "brown" }
]
}
{
_id: ObjectId("5234ccb7687ea597eabee677"),
code: "efg",
tags: [ "school", "book" ],
qty: [
{ size: "S", num: 10, color: "blue" },
{ size: "M", num: 100, color: "blue" },
{ size: "L", num: 100, color: "green" }
]
}
Is it possiblt with mongodb query? how?
I have tried many ways but didnt work. please help
I expected:
{
_id: ObjectId("5234cc8a687ea597eabee676"),
code: "abc",
tags: [ "appliance", "school", "book"],
qty: [
{ size: "6", num: 100, color: "green" },
{ size: "6", num: 70, color: "blue" },
{ size: "8", num: 100, color: "brown" }
]
}
{
_id: ObjectId("52350353b2eff1353b349de9"),
code: "ijk",
tags: [ "electronics", "school" ],
qty: [
{ size: "M", num: 100, color: "green" }
]
}
If I understood your request correctly you would like to filter on the qty.num field right? If so, use this aggregation pipeline:
var options = {
allowDiskUse: false
};
var pipeline = [
{
"$unwind": {
"path": "$qty"
}
},
{
"$match": {
"qty.num": {
"$gte": 50.0
}
}
},
{
"$group": {
"_id": "$_id",
"code": {
"$first": "$code"
},
"tags": {
"$first": "$tags"
},
"qty": {
"$push": "$qty"
}
}
}
];
var cursor = collection.aggregate(pipeline, options);
I hope this helps. It works for me.
It is confusing as the record with _id: ObjectId("52350353b2eff1353b349de9") does not appear in your sample dataset. Nevertheless, from your description, you may be looking for $allElementsTrue. You can use $map to apply your > 50 criteria to the qty array and feed the result to $allElementsTrue.
db.collection.find({
$expr: {
$allElementsTrue: {
$map: {
input: "$qty",
as: "q",
in: {
$gt: [
"$$q.num",
50
]
}
}
}
}
})
Mongo Playground
I'm trying to validate the bellow json with ajv but i'm running into problems with validating the inputs options because the validation change based on the inputs type.
I found this post which was very helpful but im still a bit stuck
My json:
{
"name": "test Template",
"canvasSize": {"width": 2480, "height": 3508},
"inputs": [
{
"hidden": true,
"name": "Background",
"type": "background",
"position": {"x": "0", "y": 0},
"value": "http://localhost:3000/Featurecompactv2.jpg",
"options": {}
}, {
"name": "Title Line",
"group": "title",
"type": "text",
"position": {"x": "center", "y": 1400},
"options": {
"fontSize": 240,
"font": "Arial Black",
"color": "black",
"textAlign": "center",
"textBaseline": "alphabetic",
"direction": "inherit"
}
},
{
"name": "Image",
"type": "image",
"position": {"x": "center", "y": 2200},
"options": {
"maxWidth": 1000,
"maxHeight": 1000
}
},
{
"name": "Barcode",
"type": "barcode",
"position": {"x": 2100, "y": 2800},
"options": {
"width": 4,
"height": 35,
"fontSize": 35
}
}
]
}
My schema so far:
const positionSchema = {
type: "object",
properties: {
x: { oneOf: [{ type: "number" }, { enum: ["center"] }] },
y: { oneOf: [{ type: "number" }, { enum: ["center"] }] },
},
};
const textOptionsSchema = {
type: "object",
properties: {
fontSize: { type: "number" },
font: { type: "string" },
color: { type: "string" },
textAlign: { type: "string" },
textBaseline: { type: "string" },
direction: { type: "string" },
prefix: { type: "string" },
disabled: { type: "boolean" },
},
additionalProperties: false,
};
const barcodeOptionsSchema = {
type: "object",
properties: {
format: {
type: "string",
enum: [
"code128",
"code128A",
"code128B",
"code128C",
"EAN2",
"EAN5",
"EAN8",
"EAN13",
"UPC",
"CODE39",
"ITF14",
"MSI",
"MSI10",
"MSI11",
"MSI1010",
"MSI1110",
"pharmacode",
"codabar",
],
},
width: { type: "number" },
height: { type: "number" },
fontSize: { type: "number" },
margin: { type: "number" },
background: { type: "string" },
displayValue: { type: "boolean" },
disabled: { type: "boolean" },
},
additionalProperties: false,
};
const imageOptionsSchema = {
type: "object",
properties: {
maxWidth: { type: "number" },
maxHeight: { type: "number" },
width: { type: "number" },
height: { type: "number" },
backgroundRemoval: { type: "boolean" },
backgroundRemovalThreshold: { type: "number" },
disabled: { type: "boolean" },
},
additionalProperties: false,
};
const backgroundOptionsSchema = {
type: "object",
properties: {
disabled: { type: "boolean" },
},
additionalProperties: false,
};
const itemSchema = {
type: "object",
properties: {
hidden: { type: "boolean" },
name: { type: "string" },
position: positionSchema,
value: { type: "string" },
placeholder: { type: "string" },
group: { type: "string" },
type: {const: "background"},
}
required: ["name", "type", "position", "options"],
additionalProperties: false,
};
const schema = {
type: "object",
properties: {
name: { type: "string" },
canvasSize: {
type: "object",
properties: {
width: { type: "number" },
height: { type: "number" },
},
},
inputs: {
type: "array",
items: itemSchema
},
},
required: ["name", "canvasSize", "inputs"],
additionalProperties: false,
};
I think i need something like this but not sure how to add it to the schema:
oneOf: [
{
properties: {
type: {const: "text"},
options: textOptionsSchema
}
},
{
properties: {
type: {const: "background"},
options: backgroundOptionsSchema
}
}
],
Or if then thing but i couldn't get either to work
I have a object like bellow
"_source": {
"name": "capsicum",
"id": "60f759b934e43100195c6142",
"userId": "60f7209ecdb2c0001935fa8f",
"version": 1,
"tags": [
{
"name": "fruit",
"id": "60f75990be3b530019062790",
"group": "location"
},
{
"name": "green",
"id": "60f75990be3b530019062766",
"group": "food"
},
{
"name": "red",
"id": "60f75990be3b530019062722",
"group": "food"
},
{
"name": "vegetable",
"id": "60f75990be3b53001906272d",
"group": "food"
}
]
}
how can I delete one element from tags, add an element in tags, in separate api calls.
I tried bellow code for deleting but not working
elasticClient.updateByQuery(
{
index: ElasticIndexs.Products,
body: {
script: {
source: `ctx._source.tags.removeIf(a -> a.id == params.id)`,
params: {
id: product.tag.id,
},
},
query: {
match: {
id: product.id,
},
},
},
},
function (error, response) {
console.log(response);
}
);
for adding:
await elasticWrapper.client.update({
index: ElasticIndexs.Products,
id: data.id,
body: {
script: {
inline: 'ctx._source.tags.add(params.tag)',
// lang: 'painless',
params: {
tag: data.tag,
},
},
},
});
for deleting:
await elasticWrapper.client.update({
index: ElasticIndexs.Products,
id: data.id,
body: {
script: {
inline: `for (int i = 0; i < ctx._source.tags.size();
i++){if(ctx._source.tags[i].id == params.id){ctx._source.tags.remove(i);}}`,
params: {
id: data.tag.id,
},
},
},
});
Looking to update a rows defined select values initialized editParams values to an alternate set based on the selected value of another cell. In the sample code you'll see if a user selects a module I am trying to update the available permissions to configure on the selected module. Any help is much appreciated.
I looked at the objects in console log and do not see any way to do this.
`function rolesTable(rolesTableName) {
var rolesTable;
setTimeout(function() {
rolesTable = new Tabulator(rolesTableName, {
data:orgRolesData,
height:"311px",
layout:"fitColumns",
placeholder:"No Data Set",
addRowPos:"top",
cellEdited:function(cell){
var data = cell.getData();
var row = cell.getRow();
if (data['module'] == "Organization") {
/*
How to set the actions select values for the row
?? row.C
*/
}
var column = cell.getColumn();
console.log(row);
},
columns:[
{title:"Name", field:"name", sorter:"string", width:200, editor:"input"},
{title:"Module", field:"module", sorter:"string", editor:"select", editorParams:{values:{"Organization":"Organization", "User":"User", "Event":"Event", "Asset":"Asset"}} },
{title:"Action", field:"action", sorter:"string", editor:"select", editorParams:{values:{"A":"A", "B":"B", "C":"C"}} },
{title:"Create", field:"create", align:"center", formatter:"tickCross", sorter:"boolean", editor:true},
{title:"Read", field:"read", align:"center", formatter:"tickCross", sorter:"boolean", editor:true},
{title:"Update", field:"update", align:"center", formatter:"tickCross", sorter:"boolean", editor:false},
{title:"Delete", field:"delete", align:"center", formatter:"tickCross", sorter:"boolean", editor:false},
],
initialSort:[
{column:"name", dir:"asc"}, //sort by this first
{column:"module", dir:"desc"}, //then sort by this second
]
});
rolesTable.redraw();
}, 800);
}`
const tabledata = [{
dep: "A",
name: "Oli Bob",
score: 100
}];
const orignalColumns = [{
title: "Name",
field: "name",
sorter: "string",
width: 200,
editor: "input"
},
{
title: "Module",
field: "module",
sorter: "string",
editor: "select",
editorParams: {
values: {
"Organization": "Organization",
"User": "User",
"Event": "Event",
"Asset": "Asset"
}
}
},
{
title: "Action",
field: "action",
sorter: "string",
editor: "select",
editorParams: {
values: {
"A": "A",
"B": "B",
"C": "C"
}
}
},
{
title: "Create",
field: "create",
align: "center",
formatter: "tickCross",
sorter: "boolean",
editor: true
},
{
title: "Read",
field: "read",
align: "center",
formatter: "tickCross",
sorter: "boolean",
editor: true
},
{
title: "Update",
field: "update",
align: "center",
formatter: "tickCross",
sorter: "boolean",
editor: false
},
{
title: "Delete",
field: "delete",
align: "center",
formatter: "tickCross",
sorter: "boolean",
editor: false
},
];
const modifieldColumns = [{
title: "Name",
field: "name",
sorter: "string",
width: 200,
editor: "input"
},
{
title: "Module",
field: "module",
sorter: "string",
editor: "select",
editorParams: {
values: {
"Organization": "Organization"
}
}
},
{
title: "Action",
field: "action",
sorter: "string",
editor: "select",
editorParams: {
values: {
"A": "A",
"B": "B",
"C": "C"
}
}
},
{
title: "Create",
field: "create",
align: "center",
formatter: "tickCross",
sorter: "boolean",
editor: true
},
{
title: "Read",
field: "read",
align: "center",
formatter: "tickCross",
sorter: "boolean",
editor: true
},
{
title: "Update",
field: "update",
align: "center",
formatter: "tickCross",
sorter: "boolean",
editor: false
},
{
title: "Delete",
field: "delete",
align: "center",
formatter: "tickCross",
sorter: "boolean",
editor: false
},
];
const table = new Tabulator("#example-table", {
data: tabledata,
cellEdited: function(cell) {
const data = cell.getData();
if (data['module'] === "Organization") {
table.setColumns(modifieldColumns);
}
},
columns: orignalColumns
});
<head>
<link href="https://unpkg.com/tabulator-tables#4.3.0/dist/css/tabulator.min.css" rel="stylesheet">
<script type="text/javascript" src="https://unpkg.com/tabulator-tables#4.3.0/dist/js/tabulator.min.js"></script>
</head>
<body>
<div id="example-table"></div>
</body>
i'm trying to return an array that i'm creating.
the problem is that I don't want to use stringify since it returns brackets.
Been trying different methods but non really work, and Im not sure if its Slacks problem, or if its in my code =/
This is what I have.
let elements = [];
for (let i in response.options) {
let opt = response.options[i];
elements.push(
{
"actions" : [
{
"name": opt.label,
"text": opt.label,
"type": "button",
"value": opt.value.input.text
}]
}
);
}
payload = {
"text": "Would you like to book?",
attachments: [
{
"text": "Choose",
"fallback": "You are unable to choose a game",
"callback_id": "wopr_game",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": elements
}
]
};
console.log(payload)
my console returns this.
{ text: 'Would you like to book?',
attachments:
[ { text: 'Choose',
fallback: 'You are unable to choose a game',
callback_id: 'wopr_game',
color: '#3AA3E3',
attachment_type: 'default',
actions: [Array] } ] }
and whit stringify
{ text: 'Would you like to book?',
attachments:
[ { text: 'Choose',
fallback: 'You are unable to choose a game',
callback_id: 'wopr_game',
color: '#3AA3E3',
attachment_type: 'default',
actions: '{"actions":{"name":"Book transport","text":"Book transport","type":"button","value":"transport"}},{"actions":{"name":"Book hotel","text":"Book hotel","type":"button","value":"hotel"}},{"actions":{"name":"Travel Policy","text":"Travel Policy","type":"button","value":"Travel Policy"}}' } ] }
Any ideas on how i can return just the text so slack renders the buttons?
Thanks in advance!
Update :
If i add a hardcoded solution to the payload, the slack buttons return correct.
payload = {
attachments: [
{
title: 'Do you want to interact with my buttons?',
callback_id: '123',
attachment_type: 'default',
actions: [
{
"name": "yes",
"text": "Yes",
"value": "yes",
"type": "button",
},
{
"name": "no",
"text": "No",
"value": "no",
"type": "button",
}
]
}
]
};
So you want to set the "elements" property of the payload to just the values in the "actions.text" properties of the objects inside the array? Sounds like a job for Array.map :) When setting elements on the payload, try this instead:
"elements": elements.map(el => el.actions.text)