mongodb map reduce value undefined - json

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 }

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"
}
}
}

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"}
}}
])

MongoDB aggregation - group and retrieve ohters fileds

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

JsonPath - Extract property by name

I am trying to extract any property name containing "cpu" and "usageRatio" from the following Json using Jayway JsonPath, but so far I have been out of luck.
Checked their documentation and could not find a solution. Any ideas?
To be clear, I would like to extract cpu usage ration for all cpu's in a dynamict fashion (there can be many cores):
{
"entries" : {
"cpu.0_1.user" : {
"lastUpdateMicros" : 1493578024979676,
"value" : 10167446,
"updateType" : "MOVING_AVERAGE"
},
"cpu.0_1.idle" : {
"value" : 277255191,
"lastUpdateMicros" : 1493578024979652,
"updateType" : "MOVING_AVERAGE"
},
"disk.usr.usageRatio" : {
"value" : 0.871294498443604,
"lastUpdateMicros" : 1493578024990246,
"updateType" : "BASIC"
},
"disk.shared.usedSpace" : {
"updateType" : "BASIC",
"lastUpdateMicros" : 1493578024990318,
"value" : 707563520
},
"disk.var.log.availableSpace" : {
"lastUpdateMicros" : 1493578024990355,
"value" : 7224614912,
"updateType" : "BASIC"
},
"disk.usr.local.www.guiserverroot.usr.local.www.guiserver.availableSpace" : {
"value" : 2369658880,
"lastUpdateMicros" : 1493578024990398,
"updateType" : "BASIC"
},
"cpu.0_0.idle" : {
"value" : 265264234,
"lastUpdateMicros" : 1493578024979593,
"updateType" : "MOVING_AVERAGE"
},
"cpu.0_1.usageRatio" : {
"value" : 0.07,
"lastUpdateMicros" : 1493578024979635,
"updateType" : "MOVING_AVERAGE"
},
"cpu.0_0.usageRatio" : {
"lastUpdateMicros" : 1493578024979564,
"value" : 0.12,
"updateType" : "MOVING_AVERAGE"
}
}
Yes, I know the Json structure sucks but sadly I don't have access to change it.
/Patrik