i have a collection with this documents:
{
"_id" : BinData(3,"utMu84VFQk+p6LOEE3Ia3w=="),
"DepartDate" : ISODate("2017-08-14T02:00:00.000+02:00"),
"Origin" : {
"Code" : "MIL",
"City" : "Milano",
"Country" : "IT"
},
"Destination" : {
"Code" : "TIA",
"City" : "Tirana",
"Country" : "AL"
},
"ReturnDate" : ISODate("2017-08-21T18:05:00.000+02:00"),
"SearchDate" : ISODate("2017-07-15T02:00:00.000+02:00"),
"Duration" : 150,
"Price" : "376.79",
"PriceCents" : 37679,
"Currency" : "EUR",
},
{
"_id" : BinData(3,"utMu845468+p6Lertya3w=="),
"DepartDate" : ISODate("2017-08-14T02:00:00.000+02:00"),
"Origin" : {
"Code" : "AOI",
"City" : "Ancona",
"Country" : "IT"
},
"Destination" : {
"Code" : "TIA",
"City" : "Tirana",
"Country" : "AL"
},
"ReturnDate" : ISODate("2017-08-21T18:05:00.000+02:00"),
"SearchDate" : ISODate("2017-07-15T02:00:00.000+02:00"),
"Duration" : 100,
"Price" : "376.79",
"PriceCents" : 37679,
"Currency" : "EUR",
},
.........
.........
I want to group by ONLY Destination.City and return an object with
Destination.City, Destination.Code and count. Like this example:
/* 1 */
{
"_id" : {
"Code" : "PMO",
"City" : "Palermo",
"Country" : "IT"
},
"count" : 184564.0
},
/* 2 */
{
"_id" : {
"Code" : "CAG",
"City" : "Cagliari",
"Country" : "IT"
},
"count" : 130873.0
},
......
......
I tried with this query (i use MongoBooster):
db.parsed_data.aggregate([
{ "$match" : { "Destination.Country" : "IT", "Segments" : { "$elemMatch" : { "Carrier" : "AZ" } } } },
{ "$group" : { "_id" : { "City" : "$Destination.City"}, "count" : { "$sum" : 1 } } },
{ "$project" : {"Destination.Code" : 1, "Destination.City" : 1, "Destination.Country" : 1 }},
{ "$sort" : { "count" : -1 } },
{ "$limit" : 9 }])
It returns grouped by City but it shows only the City Name.
In this way:
/* 1 */
{
"_id" : {
"City" : "Lampedusa"
}
},
/* 2 */
{
"_id" : {
"City" : "Trieste"
}
},
This is not correct.
How can i group by ONLY the Destination.City and return an object with
Destination.City, Destination.Code and count?
Thx
Related
$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"
}
}
}
I have following collection in mongodb-
> db.games.find().pretty();
{
"_id" : ObjectId("5db06c02e08772b58596ec72"),
"name" : "Cricket",
"genre" : "sport",
"rating" : 10
}
{
"_id" : ObjectId("5db06c02e08772b58596ec73"),
"name" : "Football",
"genre" : "sport",
"rating" : 100,
"achievement" : "champion",
"games" : [
{
"score" : 20
},
{
"score" : 30
},
{
"score" : 22
},
{
"score" : 145
}
]
}
{
"_id" : ObjectId("5db06c02e08772b58596ec74"),
"name" : "Ludo",
"genre" : "indoor",
"rating" : 1
}
{
"_id" : ObjectId("5db06c02e08772b58596ec75"),
"name" : "Badminton",
"genre" : "indoor",
"rating" : 60,
"games" : [
{
"score" : 34
},
{
"score" : 12
},
{
"score" : 50
}
]
}
{
"_id" : ObjectId("5db06c02e08772b58596ec76"),
"name" : "Swimming",
"genre" : "water",
"rating" : 50
}
{
"_id" : ObjectId("5db06c02e08772b58596ec77"),
"name" : "Running",
"genre" : "atheletics",
"rating" : 70
}
{
"_id" : ObjectId("5db06c02e08772b58596ec78"),
"name" : "Shotput",
"genre" : "atheletics",
"rating" : 66
}
I want to sum of scores for each game in the collection. For this I am trying to implement map reduce as follows -
> db.games.mapReduce( function(){emit(this.name,this.score)}, function(key,values) {return Array.sum(values)}, {out:"out_scores"});
{
"result" : "out_scores",
"timeMillis" : 330,
"counts" : {
"input" : 7,
"emit" : 7,
"reduce" : 0,
"output" : 7
},
"ok" : 1
}
But the values in the resulting collection as undefined as follows -
db.out_scores.find().pretty();
{ "_id" : "Badminton", "value" : undefined }
{ "_id" : "Cricket", "value" : undefined }
{ "_id" : "Football", "value" : undefined }
{ "_id" : "Ludo", "value" : undefined }
{ "_id" : "Running", "value" : undefined }
{ "_id" : "Shotput", "value" : undefined }
{ "_id" : "Swimming", "value" : undefined }
I am expecting sum of values of scores for each game. What am I doing wrong here?
Perhaps this query might help?
db.games.aggregate(
[
{ $unwind: { path: "$games", preserveNullAndEmptyArrays: true }},
{ $group: {
_id: "$name",
score: { $sum: "$games.score" }
}}
])
Results:
{ "_id" : "Shotput", "score" : 0 }
{ "_id" : "Running", "score" : 0 }
{ "_id" : "Ludo", "score" : 0 }
{ "_id" : "Swimming", "score" : 0 }
{ "_id" : "Badminton", "score" : 96 }
{ "_id" : "Football", "score" : 217 }
{ "_id" : "Cricket", "score" : 0 }
** If still want to use mapreduce...**
The root problem is nested data. Need to refer to the data from the games.score perspective. The mapping function of map reduce requires this logic.
db.games.mapReduce( function(){
var sum_of_score = 0;
if (this.games != undefined) {
for (var i=0; i<this.games.length; i++) {
sum_of_score += this.games[i].score;
}
}
emit(this.name, sum_of_score)
}, function(key, values) { }, {out:"out_scores"});
Query:
db.out_scores.find()
Results:
{ "_id" : "Badminton", "value" : 96 }
{ "_id" : "Cricket", "value" : 0 }
{ "_id" : "Football", "value" : 217 }
{ "_id" : "Ludo", "value" : 0 }
{ "_id" : "Running", "value" : 0 }
{ "_id" : "Shotput", "value" : 0 }
{ "_id" : "Swimming", "value" : 0 }
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
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"}
}}
])
I have to convert a JSON file in a CSV file or even better, extract the JSON file content in to an Oracle table.
I gave a look into the other topics and the only one that I found is related to the conversion in HTML table.
The files are really unstructured, here an example:
{
"message-version" : "1.2",
"orcid-profile" : {
"orcid" : null,
"orcid-id" : null,
"orcid-identifier" : {
"value" : null,
"uri" : "http://orcid.org/0000-0002-3285-9536",
"path" : "0000-0002-3285-9536",
"host" : "orcid.org"
},
"orcid-deprecated" : null,
"orcid-preferences" : {
"locale" : "EN"
},
"orcid-history" : {
"creation-method" : "MEMBER_REFERRED",
"completion-date" : null,
"submission-date" : {
"value" : 1417193651961
},
"last-modified-date" : {
"value" : 1418131422749
},
"claimed" : {
"value" : true
},
"source" : null,
"deactivation-date" : null,
"verified-email" : {
"value" : true
},
"verified-primary-email" : {
"value" : true
},
"visibility" : null
},
"orcid-bio" : {
"personal-details" : {
"given-names" : {
"value" : "Marcin"
},
"family-name" : {
"value" : "Serocki"
},
"credit-name" : null,
"other-names" : null
},
"biography" : null,
"researcher-urls" : null,
"contact-details" : null,
"keywords" : null,
"external-identifiers" : null,
"delegation" : null,
"applications" : null,
"scope" : null
},
"orcid-activities" : {
"affiliations" : {
"affiliation" : [ {
"type" : "EDUCATION",
"department-name" : "Department of Pharmaceutical Technology and Biochemistry",
"role-title" : "PhD",
"start-date" : {
"year" : {
"value" : "2010"
},
"month" : {
"value" : "09"
},
"day" : {
"value" : "01"
}
},
"end-date" : null,
"organization" : {
"name" : "Gdansk University of Technology",
"address" : {
"city" : "Gdansk",
"region" : null,
"country" : "PL"
},
"disambiguated-organization" : null
},
"source" : {
"source-orcid" : {
"value" : null,
"uri" : "http://orcid.org/0000-0002-3285-9536",
"path" : "0000-0002-3285-9536",
"host" : "orcid.org"
},
"source-client-id" : null,
"source-name" : {
"value" : "Marcin Serocki"
},
"source-date" : {
"value" : 1418131402196
}
},
"created-date" : {
"value" : 1418131402196
},
"last-modified-date" : {
"value" : 1418131402196
},
"visibility" : "PUBLIC",
"put-code" : "611828"
}, {
"type" : "EDUCATION",
"department-name" : "Department of Pharmaceutical Technology and Biochemistry",
"role-title" : "MSc",
"start-date" : {
"year" : {
"value" : "2005"
},
"month" : {
"value" : "09"
},
"day" : {
"value" : "01"
}
},
"end-date" : {
"year" : {
"value" : "2010"
},
"month" : {
"value" : "07"
},
"day" : {
"value" : "01"
}
},
"organization" : {
"name" : "Gdansk University of Technology",
"address" : {
"city" : "Gdansk",
"region" : null,
"country" : "PL"
},
"disambiguated-organization" : null
},
"source" : {
"source-orcid" : {
"value" : null,
"uri" : "http://orcid.org/0000-0002-3285-9536",
"path" : "0000-0002-3285-9536",
"host" : "orcid.org"
},
"source-client-id" : null,
"source-name" : {
"value" : "Marcin Serocki"
},
"source-date" : {
"value" : 1418131329327
}
},
"created-date" : {
"value" : 1418131329327
},
"last-modified-date" : {
"value" : 1418131329327
},
"visibility" : "PUBLIC",
"put-code" : "611826"
} ]
},
"orcid-works" : null,
"funding-list" : null
},
"orcid-internal" : null,
"type" : "USER",
"group-type" : null,
"client-type" : null
},
"orcid-search-results" : null,
"error-desc" : null
}
I have no idea how to proceed?
All code is available at github.
The code is updated according to the changed did in csv in 2013 since then CSVs now have headers! The key name for the field is now the first row of the CSV.
https://github.com/vinay20045/json-to-csv
OR
You can use api
https://json-csv.com/api to convert.
Just register there get api accessibility privileges and use thier code. That's it