Create view from 2 different collections in mongoDB - json

I want to create a view from 2 different collections(info1, info2) in mongodb.
I need to have LoginId, FirstName,LastName,Email from info1 collection and GroupName,Type,MachName from info2 collection. Connecting field between info1 and info2 collections are "GroupName" and "Group" respectively
Below the collection content:
info1:
{
"GroupName" : "TEST Group1",
"LoginId" : "login1",
"FirstName" : "John",
"LastName" : "deo",
"Email" : "john.deo#xyz.com"
}
{
"GroupName" : "TEST Group2",
"LoginId" : "login1",
"FirstName" : "John",
"LastName" : "deo",
"Email" : "john.deo#xyz.com"
}
{
"GroupName" : "TEST Group2",
"LoginId" : "login2",
"FirstName" : "Mark",
"LastName" : "Clan",
"Email" : "mark.clan#xyz.com"
}
info2:
{
"Group" : "TEST Group1",
"Type" : "DEV",
"ActiveFlag" : "True",
"MachName" : "group1.xyz.net",
}
{
"Group" : "TEST Group2",
"Type" : "DEV",
"ActiveFlag" : "True",
"MachName" : "group2.xyz.net",
}
{
"Group" : "TEST Group1",
"Type" : "UAT",
"ActiveFlag" : "True",
"MachName" : "group1.xyz.net",
}
{
"Group" : "TEST Group2",
"Type" : "UAT",
"ActiveFlag" : "True",
"MachName" : "group2.xyz.net",
}
I want to have output as below from the view.
Expected output:
{
"GroupName" : ["TEST Group1", "TEST Group2"]
"LoginId" : "login1",
"FirstName" : "John",
"LastName" : "deo",
"Email" : "john.deo#xyz.com",
"Type" : "DEV",
"MachName" : ["group1.xyz.net","group2.xyz.net"]
},
{
"GroupName" : ["TEST Group1", "TEST Group2"],
"LoginId" : "login1",
"FirstName" : "John",
"LastName" : "deo",
"Email" : "john.deo#xyz.com",
"Type" : "UAT",
"MachName" : ["group1.xyz.net","group2.xyz.net"]
},
{
"GroupName" : ["TEST Group2"],
"LoginId" : "login2",
"FirstName" : "Mark",
"LastName" : "Clan",
"Email" : "mark.clan#xyz.com",
"Type" : "DEV",
"MachName" : ["group2.xyz.net"]
},
{
"GroupName" : ["TEST Group2"]
"LoginId" : "login2",
"FirstName" : "Mark",
"LastName" : "Clan",
"Email" : "mark.clan#xyz.com",
"Type" : "UAT",
"MachName" : ["group2.xyz.net"]
}
I have tried with below but not able to get the expected output. Can someone please help me to have view which should give the expected output from the 2 collections(info1,info2)?
Tried code(not working):
db.getCollection("info1").aggregate(
[
{
"$lookup" : {
"from" : "info2",
"localField" : "GroupName",
"foreignField" : "Group",
"as" : "g"
}
} ,
{ $match: { $and: [ {"g.ActiveFlag" : "True"} ] } },
{
"$project" : {
"LoginId" : "$LoginId",
"FirstName" : "$FirstName",
"LastName" : "$LastName",
"Email" : "$Email",
"machName" : "$g.MachName"
}
}
],
{
"allowDiskUse" : false
}
);

Demo - https://mongoplayground.net/p/SX3xH1v_2wQ
Use $group
$first
$push
Groups input documents by the specified _id expression and for each distinct grouping, outputs a document. The _id field of each output document contains the unique group by value. The output documents can also contain computed fields that hold the values of some accumulator expression.
db.info1.aggregate([
{
"$lookup": {
"from": "info2",
"localField": "GroupName",
"foreignField": "Group",
"as": "g"
}
},
{
$match: {
$and: [
{
"g.ActiveFlag": "True"
}
]
}
},
{
$group: {
_id: null, // you can goup by LoginId if you want
GroupName: { $push: "$GroupName" },
MachName: { $push: { $first: "$g.MachName" } },
LoginId: { $first: "$LoginId" },
FirstName: { $first: "$FirstName" },
Email: { $first: "$Email" },
Type: { $first: { $first: "$g.Type" } },
}
}
])
Update
Updated Demo - https://mongoplayground.net/p/0AgT3FJIB6P
Use $unwind on g after lookup pipeline
{ $unwind: "$g" }
Deconstructs an array field from the input documents to output a document for each element. Each output document is the input document with the value of the array field replaced by the element.
Demo - https://mongoplayground.net/p/IGzTzEbgfl0
$group: {
_id: {
Type: "$g.Type",
LoginId: "$LoginId"
}
// ....
}

Related

file_get_contents('php://input'); is not working

$json = file_get_contents('php://input');
is not returning any result
sample JSON I was supposed to get from my payment gateway is here (sorry I couldn't post sample code here please see comment)
https://uat-paco.s3.ap-southeast-1.amazonaws.com/HTML-based-docs/CorePaymentAPI/2022-04-25-Et01x9/APIs/Payment.html#_inline_response_202_data
{
"version" : "1.0.0",
"apiResponse" : {
"responseMessageID" : "fadaba8f-b02c-4ff4-a8f0-2e5ce9873185",
"responseToRequestMessageID" : "e952828c-6d19-4a08-92e4-1bc47e1945a2",
"responseCode" : "PC-B050000",
"responseDescription" : "Success",
"responseDateTime" : "2016-08-29T09:12:34.001+0000",
"responseTime" : 2400,
"marketingDescription" : "Payment is success. Thank you for purchase."
},
"data" : {
"paymentIncompleteResult" : {
"officeGroupId" : "string",
"officeId" : "string",
"transactionDateTime" : "2016-08-29T09:12:34.001+0000",
"orderNo" : "684105191128073630",
"productDescription" : "URRT55",
"invoiceNo2C2P" : "FPX054658",
"pspReferenceNo" : "W32UPVAPCP260543864232",
"controllerInternalID" : "f81d4fae-7dec-11d0-a765-00a0c91e6bf6",
"paymentStatusInfo" : {
"paymentStatus" : "A",
"paymentStep" : "PA",
"lastUpdatedDTTM" : "2016-08-29T09:12:35.001+0000"
},
"paymentType" : "CC-VI",
"paymentExpiryDateTime" : "2016-08-30T23:59:59.999+0000",
"channelCode" : "WEBPAY",
"agentCode" : "FPX_SBIA",
"mcpFlag" : "N",
"mcpDetails" : {
"mcpType" : "DCC",
"fxRate" : {
"fxRate" : 32.9,
"markUp" : 1.2,
"convertedAmount" : {
"amountText" : "000000006135",
"currencyCode" : "USD",
"decimalPlaces" : 2,
"amount" : 61.349694
},
"fxProviderId" : "FXP0001",
"fxDate" : "2016-08-29T09:12:34.001+0000"
}
},
"merchantIdForMCP" : "4572325756",
"transactionAmount" : {
"amountText" : "000000999950",
"currencyCode" : "THB",
"decimalPlaces" : 2,
"amount" : 9999.5
},
"settlementAmount" : {
"amountText" : "000000999950",
"currencyCode" : "THB",
"decimalPlaces" : 2,
"amount" : 9999.5
},
"customFieldList" : [ {
"fieldName" : "ExternalID5",
"fieldValue" : "AMADEUS"
} ],
"aresACSChallenge" : {
"transStatus" : "C",
"acsURL" : "https://uatemvco.2c2p.com/challenge",
"creq" : "Base64url encoded form data"
},
"authentication3DSVersion" : "2.1.0",
"availablePaymentTypes" : [ "CC-VI", "CC-CA" ],
"untokenizedStoredCardList" : [ {
"storedCardUniqueID" : "string",
"isExpired" : true,
"cardNumber" : "401200XXXXXX3335",
"cardExpiryMMYY" : "1124",
"cvvCode" : "123",
"payerName" : "Attasit Txxxxxx",
"issuerBankCountry" : "TH",
"issuerBankName" : "JPMORGAN CHASE BANK N.A."
} ]
},
"paymentPage" : {
"paymentPageURL" : "https://demo-paco.2c2p.com/payment/form",
"validTillDateTime" : "2016-08-29T09:12:34.001+0000"
}
}
}

What action to return parsable Json from google to servlet?

I am trying to create a workspace add-on with a couple input boxes and a submit button. The servlet simply returns http endpoints in json code for the google add-on to read the code and build the card properly. However, I cannot seem to figure out how to get the text input values. What action do I need to call on button click to return an http endpoint with parsable json to the servlet?
Here is the code for creating the card:
{"renderActions" : {
"action" : {
"navigations" : [ {
"pushCard" : {
"header" : {
"title" : "NDA Form"
},
"sections" : [ {
"widgets" : [ {
"textParagraph" : {
"text" : "Please check for proper input!"
}
}, {
"textInput" : {
"label" : "Name",
"type" : "SINGLE_LINE",
"name" : "signerName"
}
}, {
"textInput" : {
"label" : "Email",
"type" : "SINGLE_LINE",
"name" : "email"
}
}, {
"textInput" : {
"label" : "Company Name",
"type" : "SINGLE_LINE",
"name" : "compName"
}
}, {
"textInput" : {
"label" : "Address",
"type" : "SINGLE_LINE",
"name" : "address"
}
}, {
"textInput" : {
"label" : "Date",
"type" : "SINGLE_LINE",
"name" : "date"
}
}, {
"buttonList" : {
"buttons" : [ {
"text" : "Submit Form",
"color" : {
"red" : 0.38,
"green" : 0.765,
"blue" : 0.762,
"alpha" : 1.0
},
"onClick" : {
"action" : {
"function" : "https://8f74-38-101-235-211.ngrok.io/landing/process/nda",
"parameters" : [ {
"key" : "action",
"value" : "process/nda"
} ]
}
},
"disabled" : false
} ]
}
} ]
} ]
}
} ]
}}}
And here is a picture of the form on the card:
Card Form

Creating Multiple QueueConfigurations in CloudFormation

I'm currently trying to write multiple QueueConfigurations into my CloudFormation template. Each is an SQS queue that is triggered when an object is created to a specified prefix. Here's what I have so far:
{
"Resources": {
"S3Bucket": {
"Type" : "AWS::S3::Bucket",
"Properties" :
"BucketName" : { "Ref" : "paramBucketName" },
"LoggingConfiguration" : {
"DestinationBucketName" : "test-bucket",
"LogFilePrefix" : { "Fn::Join": [ "", [ { "Ref": "paramBucketName" }, "/" ] ] }
},
"NotificationConfiguration" : {
"QueueConfigurations" : [{
"Id" : "1",
"Event" : "s3:ObjectCreated:*",
"Filter" : {
"S3Key" : {
"Rules" : {
"Name" : "prefix",
"Value" : "folder1/"
}
}
},
"Queue" : "arn:aws:sqs:us-east-1:958262988361:interstate-cdc_feeder_prod_hvr_dev"
}],
"QueueConfigurations" : [{
"Id" : "2",
"Event" : "s3:ObjectCreated:*",
"Filter" : {
"S3Key" : {
"Rules" : {
"Name" : "prefix",
"Value" : "folder2/"
}
}
},
"Queue" : "arn:aws:sqs:us-east-1:958262988361:interstate-latency_hvr_dev"
}]
}
}
}
}
}
}
I've encountered the error saying Encountered unsupported property Id. I thought that by defining the ID, I would be able to avoid the Duplicate object key error.
Does anyone know how to create multiple triggers in a single CloudFormation template? Thanks for the help in advance.
It should be structured like the below, There should only be one QueueConfigurations attribute
that contains all queue configurations within it. Also the Id parameter is not a valid property.
{
"Resources": {
"S3Bucket": {
"Type" : "AWS::S3::Bucket",
"Properties" :
"BucketName" : { "Ref" : "paramBucketName" },
"LoggingConfiguration" : {
"DestinationBucketName" : "test-bucket",
"LogFilePrefix" : { "Fn::Join": [ "", [ { "Ref": "paramBucketName" }, "/" ] ] }
},
"NotificationConfiguration" : {
"QueueConfigurations" : [{
"Event" : "s3:ObjectCreated:*",
"Filter" : {
"S3Key" : {
"Rules" : {
"Name" : "prefix",
"Value" : "folder1/"
}
}
},
"Queue" : "arn:aws:sqs:us-east-1:958262988361:interstate-cdc_feeder_prod_hvr_dev"
},
{
"Event" : "s3:ObjectCreated:*",
"Filter" : {
"S3Key" : {
"Rules" : {
"Name" : "prefix",
"Value" : "folder2/"
}
}
},
"Queue" : "arn:aws:sqs:us-east-1:958262988361:interstate-latency_hvr_dev"
}]
}
}
}
}
}
}
There is more information about QueueConfiguration in the documentation.

Update deeply nested array in mongodb

I am trying to update field value in mongoose.
{
"_id" : ObjectId("5b62c772efedb6bd3f0c983a"),
"projectID" : ObjectId("0000000050e62416d0d75837"),
"__v" : 0,
"clientID" : ObjectId("00000000996b902b7c3f5efa"),
"inspection_data" : [
{
"pdf" : null,
"published" : "N",
"submissionTime" : ISODate("2018-08-02T08:57:08.532Z"),
"userID" : ObjectId("00000000cac68e3bc04643f7"),
"insSummary" : "inspected areas",
"insName" : "Infotech",
"_id" : ObjectId("5b62c772fa02622a18655e7b"),
"published_date" : ISODate("2018-08-02T08:57:22.041Z"),
"locationAspects" : [
{
"aspectname" : "Ground floor",
"_id" : ObjectId("5b62c772fa02622a18655e80"),
"comments" : [
{
"_id" : ObjectId("5b62c772fa02622a18655e81"),
"images" : [
{
"path" : "/uploads/inspection/00000000996b902b7c3f5efa/images/1533200242005-IpjLKH4XFWNEcHXa.png",
"img_name" : "1533200242005-IpjLKH4XFWNEcHXa.png",
"title" : "Fan",
"id" : "1"
},
{
"path" : "/uploads/inspection/00000000996b902b7c3f5efa/images/1533200242008-YN8IlA5yrMn3cBnn.png",
"img_name" : "1533200242008-YN8IlA5yrMn3cBnn.png",
"title" : "Box",
"id" : "2"
}
],
"comment" : [
"comment4"
],
"recommendation" : ""
}
]
}]
}
Here I want to update a title Fan in image array as table fan.
I tried $set but I don't know how to do for my db structure.
Kindly give some solution to this
**Updated:**
I tried this code:
mongo.inspection.update({"projectID" : mongoose.Types.ObjectId(req.body.project_id) },
{ "$set": {
"inspection_data.$[e1].locationAspects.$[e2].comments.$[e3].images.$[e4].title" : "TableFan"
}},
{ "arrayFilters": [
{ "e1._id": mongoose.Types.ObjectId(req.body.insId)},
{ "e2._id": mongoose.Types.ObjectId(req.body.aspectId)},
{ "e3._id": mongoose.Types.ObjectId(req.body.commentId)},
{ "e4.id": "1" }
]},function(err,response){
if(err){
console.log("error")
}
else{
console.log('Updated')
console.log(response)
}
})
db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )
Its showing updated but in my db there is no change. Is any mistake I did ?
You can try with arrayFilters in mongodb
var mongoose = require('mongoose')
Temp.update(
{ "_id" : mongoose.Types.ObjectId("5b62c772efedb6bd3f0c983a") },
{ "$set": {
"inspection_data.$[e1].locationAspects.$[e2].comments.$[e3].images.$[e4].title": "TableFan"
}},
{ "arrayFilters": [
{ "e1._id": mongoose.Types.ObjectId("5b62c772fa02622a18655e7b") },
{ "e2._id": mongoose.Types.ObjectId("5b62c772fa02622a18655e80") },
{ "e3._id": mongoose.Types.ObjectId("5b62c772fa02622a18655e81") },
{ "e4.id": "1" }
]}
)
Note: You have to cast _id to ObjectId

Mongo forEach Query

I have the JSON that you can see below and I want to sum the values of the two objects, but when I make an aggregation it returns me 0.Here you can see the query that I use; really the first line I only use it to be sure that the path works, and it does. On the other hand,when I use this path in the aggregation query it gives me the "ID" and the "COUNT" with right values,but the "SUM" is always 0 when it must be 3600.Any idea?
db.getCollection('TEST').find({"prices.year.months.day.csv.price.valPrice":1800})
db.TEST.aggregate([
{ $match: {"location.cp":"20830"}},
{$group:{_id:"20830",total:{$sum:"$prices.year.months.day.csv.price.valPrice"}, count: { $sum: 1 }
}}])
And this is the JSON:
{
"_id" : "20830:cas:S:3639",
"lodgtype" : "Casa",
"lodg" : "Motrico: country holiday home - San sebastian",
"webid" : "6107939",
"location" : {
"thcod" : "20",
"cp" : "20830",
"th" : "Gipuzkoa",
"geometry" : {
"type" : "Point",
"coordinates" : [
43.31706238,
-2.40293598
]
}
},
"prices" : {
"year" : [
{
"valYear" : "2018",
"months" : [
{
"valMonth" : "02",
"day" : [
{
"valDay" : "13",
"csv" : [
{
"valCsv" : "20180205210908_223",
"price" : [
{
"valPrice" : 1800.0
}
]
}
]
}
]
}
]
}
]
},
"reg" : {
"created" : "20180213",
"updated" : "20180213",
"viewed" : "20180213"
}
},{
"_id" : "TEST20830:cas:S:3639",
"lodgtype" : "Casa",
"lodg" : "TESTMotrico: country holiday home - San sebastian",
"webid" : "6107930",
"location" : {
"thcod" : "20",
"cp" : "20830",
"th" : "Gipuzkoa",
"geometry" : {
"type" : "Point",
"coordinates" : [
43.31706238,
-2.40293598
]
}
},
"prices" : {
"year" : [
{
"valYear" : "2018",
"months" : [
{
"valMonth" : "02",
"day" : [
{
"valDay" : "13",
"csv" : [
{
"valCsv" : "20180205210908_223",
"price" : [
{
"valPrice" : 1800.0
}
]
}
]
}
]
}
]
}
]
},
"reg" : {
"created" : "20180213",
"updated" : "20180213",
"viewed" : "20180213"
}
}
Since you've deeply nested array you've to unwind to flatten to a document structure. To count the number of matches you've to use extra group after $match with $push with $$ROOT to keep the matching data.
db.TEST.aggregate([
{"$match":{"location.cp":"20830"}},
{"$group":{
"_id":"20830",
"data":{"$push":"$$ROOT"},
"count":{"$sum":1}
}},
{"$unwind":"$data.prices.year"},
{"$unwind":"$data.prices.year"},
{"$unwind":"$data.prices.year.months"},
{"$unwind":"$data.prices.year.months.day"},
{"$unwind":"$data.prices.year.months.day.csv"},
{"$unwind":"$data.prices.year.months.day.csv.price"},
{"$group":{
"_id":"20830",
"total":{"$sum":"$prices.year.months.day.csv.price.valPrice"},
"count":{"$first":"$count"}
}}
])