I have two tables in my dynamo db one is candidate table and the other one is user table I want to use batchWriteItem in dynamo db in order to add the data in the table.
The query which I have formatted is as follows
var user = {
userid: usrid,
role: 'candidate',
password: vucrypt.encryptpass(pass)
};
var canduser = {
fname: req.body.fname,
lname: req.body.lname,
location: req.body.location,
phone: req.body.phone,
ccode: req.body.ccode,
grad: req.body.grad,
pgrad: req.body.pgrad,
ograd: req.body.ograd,
experience: exp,
linkedin: req.body.linkedin,
terms: tandc
};
canduser = vutools.fixcanduser(canduser);
canduser.userid = usrid;
var writes = {
'users': [{put: user}],
'candidate': [{put: canduser}],
};
But if i use
dynamodb.batchWriteItem(writes, function(err, regdata) {
}
Its ending up as error.
How can I write the right query? The error I am getting is this.
MultipleValidationErrors: There were 3 validation errors:
* MissingRequiredParameter: Missing required key 'RequestItems' in params
* UnexpectedParameter: Unexpected key 'users' found in params
* UnexpectedParameter: Unexpected key 'candidate' found in params
To batchwrite in DynamoDB, the data must be formated in the dynamodb way.
if you want do it in standard json, go for the documentclient.
you have an example below, have in mind that dynamobb batchwrite only accept mawimum of 25 element by request.
so according to the doc you must have :
1. Attributes
"ATTRIBUTE_1": { "S": "ATTRIBUTE_1_VALUE" }
According to your example :
"role": {"S":"candidate"}
2. Items
Each item must have this format
PutRequest: {
Item: {
...,
"ATTRIBUTE_1": { "S": "ATTRIBUTE_1_VALUE" },
...
}
}
3. Array of items to add
Create an array of items, which doesn't exceed 25 elements, (it's a dynamodb limit for batchwrite)
4. Your request params
put it together
var params = {
RequestItems: {
"TABLE_NAME": [
//the array you just created in step 3
]
}
}
5. The request
ddb.batchWriteItem(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
UPDATE
Your example will be something like this :
var params = {
"RequestItems": {
"TABLE_NAME": [
{
"PutRequest": {
Item: {
"userid": { "N": "usrid" },
"role": { "S": 'candidate' },
"password": { "S": vucrypt.encryptpass(pass) }
}
}
}
],
"TABLE_NAME2": [
{
"PutRequest": {
Item: {
"fname": {
"S": req.body.fname
},
"lname": {
"S": req.body.lname
},
"location": {
"S": req.body.location
},
"phone": {
"S": req.body.phone
},
"ccode": {
"S": req.body.ccode
},
"grad": {
"S": req.body.grad
},
"pgrad": {
"S": req.body.pgrad
},
"ograd": {
"S": req.body.ograd
},
"experience": {
"S": exp
},
"linkedin": {
"S": req.body.linkedin
},
"terms": {
"S": tandc
}
}
}
}
]
}
}
This is the right answer there are some type problems.
var createuser = {
"RequestItems": {
"users": [{
"PutRequest": {
Item: {
"userid": {
"S": usrid +""
},
"password": {
"S": vucrypt.encryptpass(pass) +""
},
"role": {
"S": 'candidate' +""
}
}
}
}],
"candidate": [{
"PutRequest": {
Item: {
"ccode": {
"S": req.body.ccode +""
},
"fname": {
"S": req.body.fname +""
},
"lname": {
"S": req.body.lname +""
},
"pgrad": {
"S": req.body.pgrad +""
},
"videoresumeurl": {
"S": "-"
},
"phone": {
"S": req.body.phone +""
},
"terms": {
"S": tandc +""
},
"location": {
"S": req.body.location +""
},
"experience": {
"N": req.body.experience +""
},
"userid": {
"S": usrid +""
},
"grad": {
"S": req.body.grad +""
}
}
}
}]
}
}
Related
My project is in nodeJs with express and i use mongoose for the request to my database mongoDb.
I have a model Media the structure is:
{
"_id": {
"$oid": "6354f982a11464ff4f7bac60"
},
"userId": {
"$oid": "6353aa119d39ccbb3263123f"
},
"date": "2022-10-23",
"base64": "data:image/png;base64,iVBORw0KGgoAAA....=",
"label": "Noamount",
"uriPath": "xxxx",
"s3Path": "xxxx",
"amount": 0,
"__v": 0,
"type": "tva"
}
Like you seen there is a field date, imagine that i have 3 medias on differents month:
{
"date": "2022-10-23",
"label": "monthTen",
"type" : "tva",
... // Other property
},
{
"date": "2022-09-10",
"label": "monthNineFirst",
"type" : "tva",
... // Other property
},
{
"date": "2022-09-19",
"label": "monthNineSecond",
"type" : "other",
... // Other property
}
I want to output something like this:
// Ordery by type in first place and by month
// number like 9 = the month inside the date property
{
tva: {
9: [{ label: "monthNineFirst"... }],
10: [{ label: "monthNineTen"... }]
},
other: {
9: [{ label: "monthNineSecond"... }]
}
}
Is found the property aggregate but i don't understand it correctly.
I know how to dow that in JS it's easy, but can i do that directly on the request with the property aggregate and $group?
There is what i have done so far https://mongoplayground.net/p/-vAbdnnqOfD
Here's one way to do it by extending your mongoplayground.net start.
db.collection.aggregate([
{ // group all docs by month
$group: {
_id: {
$month: {
$dateFromString: {
dateString: "$date",
format: "%Y-%m-%d"
}
}
},
data: {"$push": "$$ROOT"}
}
},
{ // group all groups into a single doc
"$group": {
"_id": null,
"groupData": {
"$push": {
// k,v for $arrayToObject
"k": "$_id",
"v": {
"$sortArray": {
"input": "$data",
"sortBy": {"date": 1}
}
}
}
}
}
},
{
"$replaceWith": {
"$arrayToObject": {
"$map": {
"input": {
// sort by month
"$sortArray": {
"input": "$groupData",
"sortBy": {"k": 1}
}
},
"in": {
"$mergeObjects": [
"$$this",
{ // rewrite k as string
"k": {"$toString": "$$this.k"}
}
]
}
}
}
}
}
])
Try it on mongoplayground.net.
I'm having kind of complex nested JSON in my dynamoDB, I want to extract this Filter object and its values. For example, if Type is macBook return the Filter object and its value PRO and AIR. Please find my code below and JSON input - I'm struggling to parse these value in NodeJS - could someone please help me - how to get the Filter Object?
JSON
{
"ProductType": {
"S": "Apple"
},
"Type": {
"M": {
"iPhone": {
"M": {
"Filter": {
"M": {
"model": {
"SS": [
"X", "XS"
]
}
}
}
}
},
"macBook": {
"M": {
"Filter": {
"M": {
"model": {
"SS": [
"PRO", "AIR"
]
}
}
}
}
}
}
}
}
product.js
const AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({
region: process.env.AWS_REGION,
});
const getModel = function (productType) {
return new Promise((resolve, reject) => {
var params = {
Key: {
"ProductType": {
S: productType
}
},
TableName: 'product'
};
dynamodb.getItem(params, function (err, data) {
if (err) reject(err, err.stack);
else {
console.log("data: " + Object.keys(data));
let product = data.Item.ProductType.S;
let filterModel = data.Item.Type.M
console.log(filterModel);
}
});
});
}
getModel('Apple')
Any help would be much appreciated
You need to use Object.values to get the values of iPhone and macbook objects, then inside you can use flatMap to find the Filter whose has PRO", "AIR" properties.
const data ={
"ProductType": {
"S": "Apple"
},
"Type": {
"M": {
"iPhone": {
"M": {
"Filter": {
"M": {
"model": {
"SS": [
"X", "XS"
]
}
}
}
}
},
"macBook": {
"M": {
"Filter": {
"M": {
"model": {
"SS": [
"PRO", "AIR"
]
}
}
}
}
}
}
}
}
const result = Object.values(data.Type.M).flatMap((item) => {
const SS = item.M.Filter.M.model.SS
return SS.includes('PRO') && SS.includes('AIR') ? [{ Filter: item.M.Filter.M }] : []
}, [])
console.log(result)
I want to know how I add something with the writeFile instead of overwriting it.
This is what I expected:
{
"726623241984278589": {
"322366672625467392": {
"": {
"Moderation": "Kick",
"TargetID": "322366672625467392",
"Moderator": "Bobosky#3914",
"Reason": "Testing",
"ID": "#ZAHVOJUZ"
}
"2": {
"Moderation": "Kick",
"TargetID": "322366672625467392",
"Moderator": "Bobosky#3914",
"Reason": "Testing",
"ID": "#KX7OMT4S"
},
"3": {
"Moderation": "Kick",
"TargetID": "322366672625467392",
"Moderator": "Bobosky#3914",
"Reason": "Testing",
"ID": "#LLV26ZV8"
}
}
},
"Count": {
"322366672625467392": 0
}
}
But it outputs like this:
{
"726623241984278589": {
"322366672625467392": {
"3": {
"Moderation": "Kick",
"TargetID": "322366672625467392",
"Moderator": "Bobosky#3914",
"Reason": "Testing",
"ID": "#LLV26ZV8"
}
}
},
"Count": {
"322366672625467392": 0
}
}
And there is my code:
if (!data['Count']) data['Count'] = {
[TargetMember.id]: 0
};
data['Count'][TargetMember.id]++;
const Counter = data['Count'][TargetMember.id];
data[message.guild.id] = {
[TargetMember.id]: {
[Counter]: {
Moderation: 'Kick',
TargetID: `${TargetMember.id}`,
Moderator: `${message.author.tag}`,
Reason: `${Reason}`,
ID: `#${ID}`
}
}
}
fs.writeFile("./data.json", JSON.stringify(data), (err) => {
if (err) console.log(err);
});
How can I add data besides of the old data instead of overwriting them?
PS: I didn't show all the codes since some of them are irrelevant to this question. And all the parmas works fine such as the TargetMember, reason etc. I just want to know why I can't add data instead of overwriting them. And the project is based on discord.js
instead of writeFile method you have to use appendFile
fs.appendFile(path, data[, options], callback)
fs.appendFile("./data.json", JSON.stringify(data), (err) => {
if (err) console.log(err);
});
documentation:
https://nodejs.org/api/fs.html#fs_fs_appendfile_path_data_options_callback
I have an object received in response from backend, and would like to extract elements of object and attach them to scope to make it available in the View.
Following is the structure of the object:
{
"Name": {
"S": "Jon Snow"
},
"Location": {
"S": "Winterfell"
},
"Details": {
"M": {
"Parents": {
"M": {
"mother": {
"S": "Lynna Stark"
}
}
},
"Dog": {
"N": "Ghost Snow"
}
}
}
}
Since I have received it from backend, I don't know what kind of object is this, and I want to convert it to a plain JSON object which should be looking something like this:
{
"Name": "Jon Snow",
"Location": "Winterfell",
"Details": {
"Parents": {
"mother": "Lynna Stark"
},
"Dog": "Ghost Snow"
}
}
Help is appreciated, as I am a beginner in AngularJS and also It would be good if someone elaborates what kind of Object did I receive? Thanks in advance.
Update 1:
Thanks for the responses. Now I have got the idea. Now the question is how to I flatten the object by one level? And If I do flatten does it tamper the original response as it is received from the backend, it may be different every time.
const data = {
"Name": {
"S": "Jon Snow"
},
"Location": {
"S": "Winterfell"
},
"Details": {
"M": {
"Parents": {
"M": {
"mother": {
"S": "Lynna Stark"
}
}
},
"Dog": {
"N": "Ghost Snow"
}
}
}
}
const flatten = (data) =>{
if(typeof data === "string") return data;
for(let key in data){
for(let deep in data[key]){
if(deep.length === 1){
const temp = data[key]
data[key] = flatten(temp[deep])
}
}
}
return data;
}
console.log( JSON.stringify(flatten(data), null, "\t"))
JS bin
I need to find out if one user appears more than once in the associative array (and then sum the value of the same tasks). How do I do that in javaScript?
{
"Items": [
{
"Date": {
"N": "1439883817221"
},
"UserName": {
"S": "user1"
},
"task1": {
"N": "9"
}
},
{
"Date": {
"N": "1439892361108"
},
"task2": {
"N": "3"
},
"UserName": {
"S": "user2"
}
},
{
"Date": {
"N": "1439904242126"
},
"UserName": {
"S": "user1"
},
"task2": {
"N": "2"
}
}
}}
Try this:
var uniqueNames = [];
var nonUniqueNames = [];
$.each( list.Items, function(i, item ) {
if(uniqueNames.indexOf(item.UserName.S) != -1 ){
nonUniqueNames.push(item.UserName.S);
} else {
uniqueNames.push(item.UserName.S);
}
});
alert(nonUniqueNames);
https://jsfiddle.net/c2co1hck/
As for the same tasks you can figure out the same way to do it.
Please define the same tasks anyway as you have different names and values for it.
Posting some of your code would also be appreciated.
By the way you have an error in your JSON, it should end with
]} not with }}