Group by field doesn't work in MongoDB - php

I'm trying to calculate a sum of all records in my db, and I need to avoid duplicates. I wrote this code to group the records, but it doesn't work for me.
$pipeline = [
['$match' =>
$criteria->getCondition()],
['$group' =>
['_id' => '$order_id', 'total' => ['$sum' => '$'.$column]]]
];
$this->getDbConnection()->aggregate('ticket_cache', $pipeline);
Test request:
db.getCollection('ticket_cache').aggregate(
{
"$match":
{"event_id":64}
},
{
"$group" :
{"_id":"$order_id", "total": {"$sum":"$payment_amount"}}
})
Result:
/* 1 */
{
"result" : [
{
"_id" : NumberLong(7002),
"total" : 9000.0000000000000000
}
],
"ok" : 1.0000000000000000
}
Data in the db:
/* 1 */
{
"result" : [
{
"_id" : ObjectId("553f8b4fbfabe2772f8b4f51"),
"event_id" : NumberLong(64),
"ticket_id" : NumberLong(8563),
"ticket_code" : NumberLong(22062299),
"ticket_type_id" : NumberLong(391),
"ticket_created" : NumberLong(1430227620),
"ticket_deleted" : NumberLong(0),
"ticket_user_id" : NumberLong(2),
"ticket_used" : NumberLong(0),
"order_id" : NumberLong(7002),
"order_code" : NumberLong(517005),
"order_created" : NumberLong(1430227620),
"order_deleted" : NumberLong(0),
"order_sales_pipeline" : NumberLong(18),
"order_invoice_id" : NumberLong(4202),
"order_invoice_amount" : 3000.0000000000000000,
"order_invoice_created" : NumberLong(1430227641),
"order_invoice_deleted" : NumberLong(0),
"order_invoice_code" : NumberLong(420155),
"payment_id" : NumberLong(4365),
"payment_amount" : 3000.0000000000000000,
"payment_currency" : NumberLong(4),
"payment_author_id" : NumberLong(1),
"payment_type_id" : NumberLong(27),
"payment_created" : NumberLong(1430227641),
"payment_deleted" : NumberLong(0),
"create_time" : ISODate("2015-04-28T13:29:51.328Z")
},
{
"_id" : ObjectId("553f8b4fbfabe2772f8b4f4f"),
"event_id" : NumberLong(64),
"ticket_id" : NumberLong(8561),
"ticket_code" : NumberLong(49287433),
"ticket_type_id" : NumberLong(391),
"ticket_created" : NumberLong(1430227620),
"ticket_deleted" : NumberLong(0),
"ticket_user_id" : NumberLong(2),
"ticket_used" : NumberLong(0),
"order_id" : NumberLong(7002),
"order_code" : NumberLong(517005),
"order_created" : NumberLong(1430227620),
"order_deleted" : NumberLong(0),
"order_sales_pipeline" : NumberLong(18),
"order_invoice_id" : NumberLong(4202),
"order_invoice_amount" : 3000.0000000000000000,
"order_invoice_created" : NumberLong(1430227641),
"order_invoice_deleted" : NumberLong(0),
"order_invoice_code" : NumberLong(420155),
"payment_id" : NumberLong(4365),
"payment_amount" : 3000.0000000000000000,
"payment_currency" : NumberLong(4),
"payment_author_id" : NumberLong(1),
"payment_type_id" : NumberLong(27),
"payment_created" : NumberLong(1430227641),
"payment_deleted" : NumberLong(0),
"create_time" : ISODate("2015-04-28T13:29:51.316Z")
},
{
"_id" : ObjectId("553f8b4fbfabe2772f8b4f50"),
"event_id" : NumberLong(64),
"ticket_id" : NumberLong(8562),
"ticket_code" : NumberLong(24016753),
"ticket_type_id" : NumberLong(391),
"ticket_created" : NumberLong(1430227620),
"ticket_deleted" : NumberLong(0),
"ticket_user_id" : NumberLong(2),
"ticket_used" : NumberLong(0),
"order_id" : NumberLong(7002),
"order_code" : NumberLong(517005),
"order_created" : NumberLong(1430227620),
"order_deleted" : NumberLong(0),
"order_sales_pipeline" : NumberLong(18),
"order_invoice_id" : NumberLong(4202),
"order_invoice_amount" : 3000.0000000000000000,
"order_invoice_created" : NumberLong(1430227641),
"order_invoice_deleted" : NumberLong(0),
"order_invoice_code" : NumberLong(420155),
"payment_id" : NumberLong(4365),
"payment_amount" : 3000.0000000000000000,
"payment_currency" : NumberLong(4),
"payment_author_id" : NumberLong(1),
"payment_type_id" : NumberLong(27),
"payment_created" : NumberLong(1430227641),
"payment_deleted" : NumberLong(0),
"create_time" : ISODate("2015-04-28T13:29:51.326Z")
}
],
"ok" : 1.0000000000000000
}
Where did I go wrong?

Can you try with the below query. It assumes that the payment amount will always be same. Have a look at addToSet http://docs.mongodb.org/manual/reference/operator/update/addToSet/.
db.getCollection('ticket_cache').aggregate(
{ "$match": {"event_id":64} },
{ "$group" :
{"_id":"$order_id", "total": {"$addToSet":"$payment_amount"}}
},
{"$unwind": "$total"},
{"$group": {"_id": "null", "totalOdr": {"$sum": "$total"}}}
)

Related

Mongodb aggregation timeout

I have a large collection about 200 mil documents. Documents are small but if I try to aggregate some data it fall down on timeout error. I am not sure if it is caused by indexes or by documents count.
The pipeline looks like:
{
$match: {
global_campaign_id: {$in: [3151 ,3207 ,3208 ,3209 ,3210 , ... (30 ids)]}
}
},
{
$group: {
_id: {
global_campaign_id: "$global_campaign_id",
device_id: "$device_id",
partner_id: "$partner_id"
},
date_last: {
$max: "$date_created"
},
partner_id: {$first: "$partner_id"},
campaign_id: {$first: "$global_campaign_id"},
device_id: {$first: "$device_id"}
}
}
Then I have this idexes on the collection:
_id: 1
date_created: 1,
global_campaign_id: 1,
global_campaign_id: 1, date_created: 1,
global_campaign_id: 1, device_id: 1, partner_id: 1, date_created: 1
What could be the problem? I need last document for every group. Could sort stage before group help?
Here is result of explain command
{
"stages" : [
{
"$cursor" : {
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "lurity.impressions",
"indexFilterSet" : false,
"parsedQuery" : {
"global_campaign_id" : {
"$in" : [
3151,
3207,
3208,
3209,
3210
]
}
},
"queryHash" : "901B446C",
"planCacheKey" : "06CC82FF",
"winningPlan" : {
"stage" : "PROJECTION_COVERED",
"transformBy" : {
"date_created" : 1,
"device_id" : 1,
"global_campaign_id" : 1,
"partner_id" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"global_campaign_id" : 1,
"device_id" : 1,
"partner_id" : 1,
"date_created" : 1
},
"indexName" : "global_campaign_id_1_device_id_1_partner_id_1_date_created_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"global_campaign_id" : [ ],
"device_id" : [ ],
"partner_id" : [ ],
"date_created" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"global_campaign_id" : [
"[3151.0, 3151.0]",
"[3207.0, 3207.0]",
"[3208.0, 3208.0]",
"[3209.0, 3209.0]",
"[3210.0, 3210.0]"
],
"device_id" : [
"[MinKey, MaxKey]"
],
"partner_id" : [
"[MinKey, MaxKey]"
],
"date_created" : [
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans" : [
{
"stage" : "PROJECTION_SIMPLE",
"transformBy" : {
"date_created" : 1,
"device_id" : 1,
"global_campaign_id" : 1,
"partner_id" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"global_campaign_id" : 1
},
"indexName" : "global_campaign_id_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"global_campaign_id" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"global_campaign_id" : [
"[3151.0, 3151.0]",
"[3207.0, 3207.0]",
"[3208.0, 3208.0]",
"[3209.0, 3209.0]",
"[3210.0, 3210.0]"
]
}
}
}
},
{
"stage" : "PROJECTION_SIMPLE",
"transformBy" : {
"date_created" : 1,
"device_id" : 1,
"global_campaign_id" : 1,
"partner_id" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"global_campaign_id" : 1,
"date_created" : 1
},
"indexName" : "global_campaign_id_1_date_created_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"global_campaign_id" : [ ],
"date_created" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"global_campaign_id" : [
"[3151.0, 3151.0]",
"[3207.0, 3207.0]",
"[3208.0, 3208.0]",
"[3209.0, 3209.0]",
"[3210.0, 3210.0]"
],
"date_created" : [
"[MinKey, MaxKey]"
]
}
}
}
}
]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 304244,
"executionTimeMillis" : 1363,
"totalKeysExamined" : 304246,
"totalDocsExamined" : 0,
"executionStages" : {
"stage" : "PROJECTION_COVERED",
"nReturned" : 304244,
"executionTimeMillisEstimate" : 112,
"works" : 304246,
"advanced" : 304244,
"needTime" : 1,
"needYield" : 0,
"saveState" : 322,
"restoreState" : 322,
"isEOF" : 1,
"transformBy" : {
"date_created" : 1,
"device_id" : 1,
"global_campaign_id" : 1,
"partner_id" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 304244,
"executionTimeMillisEstimate" : 73,
"works" : 304246,
"advanced" : 304244,
"needTime" : 1,
"needYield" : 0,
"saveState" : 322,
"restoreState" : 322,
"isEOF" : 1,
"keyPattern" : {
"global_campaign_id" : 1,
"device_id" : 1,
"partner_id" : 1,
"date_created" : 1
},
"indexName" : "global_campaign_id_1_device_id_1_partner_id_1_date_created_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"global_campaign_id" : [ ],
"device_id" : [ ],
"partner_id" : [ ],
"date_created" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"global_campaign_id" : [
"[3151.0, 3151.0]",
"[3207.0, 3207.0]",
"[3208.0, 3208.0]",
"[3209.0, 3209.0]",
"[3210.0, 3210.0]"
],
"device_id" : [
"[MinKey, MaxKey]"
],
"partner_id" : [
"[MinKey, MaxKey]"
],
"date_created" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 304246,
"seeks" : 2,
"dupsTested" : 0,
"dupsDropped" : 0
}
}
}
},
"nReturned" : NumberLong(304244),
"executionTimeMillisEstimate" : NumberLong(803)
},
{
"$group" : {
"_id" : {
"global_campaign_id" : "$global_campaign_id",
"device_id" : "$device_id",
"partner_id" : "$partner_id"
},
"date_last" : {
"$max" : "$date_created"
},
"partner_id" : {
"$first" : "$partner_id"
},
"campaign_id" : {
"$first" : "$global_campaign_id"
},
"device_id" : {
"$first" : "$device_id"
}
},
"nReturned" : NumberLong(84),
"executionTimeMillisEstimate" : NumberLong(1358)
}
],
"serverInfo" : {
"host" : "nosql-lurity",
"port" : 27017,
"version" : "4.4.13",
"gitVersion" : "df25c71b8674a78e17468f48bcda5285decb9246"
},
"ok" : 1
}

How to parse Google Directions API Json in PHP, specifically the Distance-Values and Duration-Values for each "Step" and "Leg" which are all nested?

The json response can have any number of Distance-Values, Duration-Values, Steps and Legs nested within each other. I need to ensure the original pairing of Distance and Duration in a variable for later math. Note the file also has Distance-Values and Duration-Values NOT nested within the Steps that I am not interested in collecting. I am a newbie PHP programmer - please be gentle. Thank you.
Here is a sample json file response, apologies it is so long.
{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"partial_match" : true,
"place_id" : "ChIJq0F7QdOGwokRtGWVrOp28h0",
"types" : [ "premise" ]
},
{
"geocoder_status" : "OK",
"partial_match" : true,
"place_id" : "ChIJvzh05jM_6IkRhcy9aqUcTHM",
"types" : [ "establishment", "point_of_interest", "university" ]
},
{
"geocoder_status" : "OK",
"partial_match" : true,
"place_id" : "ChIJ28a23cMv6IkRLzOqZ0h6vwo",
"types" : [ "premise" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 40.9003613,
"lng" : -73.11792029999999
},
"southwest" : {
"lat" : 40.7791055,
"lng" : -73.5611243
}
},
"copyrights" : "Map data ©2020 Google",
"legs" : [
{
"distance" : {
"text" : "28.7 mi",
"value" : 46179
},
"duration" : {
"text" : "40 mins",
"value" : 2424
},
"end_address" : "100 Nicolls Rd, Stony Brook, NY 11794, USA",
"end_location" : {
"lat" : 40.9003613,
"lng" : -73.1305969
},
"start_address" : "99 Jericho Turnpike, Westbury, NY 11590, USA",
"start_location" : {
"lat" : 40.7791055,
"lng" : -73.56088389999999
},
"steps" : [
{
"distance" : {
"text" : "128 ft",
"value" : 39
},
"duration" : {
"text" : "1 min",
"value" : 14
},
"end_location" : {
"lat" : 40.7794066,
"lng" : -73.5611243
},
"html_instructions" : "Head \u003cb\u003enorthwest\u003c/b\u003e toward \u003cb\u003eJericho Turnpike\u003c/b\u003e",
"polyline" : {
"points" : "mt{wFnj~_M{#n#"
},
"start_location" : {
"lat" : 40.7791055,
"lng" : -73.56088389999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.4 mi",
"value" : 629
},
"duration" : {
"text" : "3 mins",
"value" : 158
},
"end_location" : {
"lat" : 40.7821022,
"lng" : -73.5545772
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eJericho Turnpike\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "iv{wF~k~_Me#oAg#wAQe#M_#]_Ag#sAQe#g#wA[s#K[a#eAWu#Qs#I[m#gCi#{BAEk#{Bg#yB"
},
"start_location" : {
"lat" : 40.7794066,
"lng" : -73.5611243
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.2 mi",
"value" : 251
},
"duration" : {
"text" : "1 min",
"value" : 15
},
"end_location" : {
"lat" : 40.7827402,
"lng" : -73.55172809999999
},
"html_instructions" : "Slight \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eS Marginal Rd\u003c/b\u003e",
"maneuver" : "turn-slight-right",
"polyline" : {
"points" : "cg|wFbc}_MEe#AGAECKIc#I_#IYEY[mBKy#Ik#Ge#E_#EYAYCYAY"
},
"start_location" : {
"lat" : 40.7821022,
"lng" : -73.5545772
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.1 mi",
"value" : 218
},
"duration" : {
"text" : "1 min",
"value" : 13
},
"end_location" : {
"lat" : 40.7831579,
"lng" : -73.54922080000001
},
"html_instructions" : "Take the \u003cb\u003eInterstate 495 E\u003c/b\u003e ramp on the \u003cb\u003eleft\u003c/b\u003e to \u003cb\u003eRiverhead\u003c/b\u003e",
"maneuver" : "ramp-left",
"polyline" : {
"points" : "ck|wFhq|_MIYAEAICMASAMKeAKuAC]Cm#AAAYCi#Cs#AK?GAIAGACCEEI"
},
"start_location" : {
"lat" : 40.7827402,
"lng" : -73.55172809999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "1.3 mi",
"value" : 2154
},
"duration" : {
"text" : "1 min",
"value" : 83
},
"end_location" : {
"lat" : 40.7877687,
"lng" : -73.5245628
},
"html_instructions" : "Merge onto \u003cb\u003eI-495 E\u003c/b\u003e",
"maneuver" : "merge",
"polyline" : {
"points" : "wm|wFra|_MW{MWeIEq#]eIWeFOcCG{#UmD_#cFU}BQaBQaBQeBuAsMc#iDGe#Gi#c#}Cg#mDg#cD[mBc#kCYgB]mBa#cC_#kBi#eC[kA[mAa#yAOg#Wy#Qi#]eA"
},
"start_location" : {
"lat" : 40.7831579,
"lng" : -73.54922080000001
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.4 mi",
"value" : 679
},
"duration" : {
"text" : "1 min",
"value" : 34
},
"end_location" : {
"lat" : 40.79059609999999,
"lng" : -73.5181288
},
"html_instructions" : "Take exit \u003cb\u003e42\u003c/b\u003e for \u003cb\u003eBroadway\u003c/b\u003e/\u003cwbr/\u003e\u003cb\u003eNorthern Pkwy E\u003c/b\u003e toward \u003cb\u003eHauppauge\u003c/b\u003e",
"maneuver" : "ramp-right",
"polyline" : {
"points" : "qj}wFngw_MAa#AGK[OIOGMEMOC"
},
"start_location" : {
"lat" : 40.7877687,
"lng" : -73.5245628
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "16.0 mi",
"value" : 25810
},
"duration" : {
"text" : "16 mins",
"value" : 945
},
"end_location" : {
"lat" : 40.829462,
"lng" : -73.25121489999999
},
"html_instructions" : "Merge onto \u003cb\u003eNorthern State Pkwy\u003c/b\u003e",
"maneuver" : "merge",
"polyline" : {
"points" : "g|}wFh_v_Me#wAK[a#]#[#[#]#YBWBU#W#CB]DYD_#NaB"
},
"start_location" : {
"lat" : 40.79059609999999,
"lng" : -73.5181288
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.3 mi",
"value" : 463
},
"duration" : {
"text" : "1 min",
"value" : 18
},
"end_location" : {
"lat" : 40.8278228,
"lng" : -73.2461648
},
"html_instructions" : "Merge onto \u003cb\u003eNY-347\u003c/b\u003e/\u003cwbr/\u003e\u003cb\u003eNY-454 E\u003c/b\u003e",
"maneuver" : "merge",
"polyline" : {
"points" : "coexF`{a~LBQBOF_#H_#F[Ha#H_#Ji#Je#H_#XmA\\oAVkA~CwM"
},
"start_location" : {
"lat" : 40.829462,
"lng" : -73.25121489999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "1.8 mi",
"value" : 2820
},
"duration" : {
"text" : "3 mins",
"value" : 163
},
"end_location" : {
"lat" : 40.8243168,
"lng" : -73.21340239999999
},
"html_instructions" : "Continue straight to stay on \u003cb\u003eNY-347\u003c/b\u003e/\u003cwbr/\u003e\u003cb\u003eNY-454 E\u003c/b\u003e",
"maneuver" : "straight",
"polyline" : {
"points" : "{dexFn{`~L^_Bf#aCLi#b#{BlAyF^kB`#kBFWTeAd#gC`#gCN{#l#cF?IJcAPcCNoB`#eFTwCFcAXkD#[Dq#|#aMB[XiDVkDDw#NiCJyCHgD#gA#qA?c#?uAA[AqA?WC}#K{DEqAGmCAc#CqAGkBMcFA_#E_ACaACaAIoBA]A}A?qB?Y#oDH_EDkBVsDJsA"
},
"start_location" : {
"lat" : 40.8278228,
"lng" : -73.2461648
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "6.2 mi",
"value" : 9936
},
"duration" : {
"text" : "11 mins",
"value" : 689
},
"end_location" : {
"lat" : 40.8747687,
"lng" : -73.12067499999999
},
"html_instructions" : "Keep \u003cb\u003eleft\u003c/b\u003e to continue on \u003cb\u003eNY-347 E\u003c/b\u003e, follow signs for \u003cb\u003ePort Jefferson\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Carvel (on the right)\u003c/div\u003e",
"maneuver" : "keep-left",
"polyline" : {
"points" : "_odxFvnz}LFaD#o#Iw[q#yAeD_AqBk#oA{#mBoBmEc#cAoDwHQ_#"
},
"start_location" : {
"lat" : 40.8243168,
"lng" : -73.21340239999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.2 mi",
"value" : 281
},
"duration" : {
"text" : "1 min",
"value" : 34
},
"end_location" : {
"lat" : 40.8763118,
"lng" : -73.1180346
},
"html_instructions" : "Continue onto \u003cb\u003eSmithtown Bypass\u003c/b\u003e",
"polyline" : {
"points" : "ijnxFdkh}LcAuBQ]eCyEo#wAg#iA"
},
"start_location" : {
"lat" : 40.8747687,
"lng" : -73.12067499999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "1.8 mi",
"value" : 2899
},
"duration" : {
"text" : "4 mins",
"value" : 258
},
"end_location" : {
"lat" : 40.9003613,
"lng" : -73.1305969
},
"html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eStony Brook Rd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDestination will be on the right\u003c/div\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "}snxFtzg}LIU]TmBx#WJo#XiAd#IDqB|#sAj#sF`CoFzBaHvCmF|BaDrAw#`#wDbBiAt#gBpAMNo#d#_#TiBpAcAt#cCbB]VUN}BxAUNe#VoAp#aCnAcDdBiAl#}Ax#MHi#Tc#PkA\\wCv#yA`#{Ab#qAZqAVkCf#_BZWFiB\\uAXiB`#e#JeB\\[HMBSFA?WJ_#Tc#ZML"
},
"start_location" : {
"lat" : 40.8763118,
"lng" : -73.1180346
},
"travel_mode" : "DRIVING"
}
],
"traffic_speed_entry" : [],
"via_waypoint" : []
},
{
"distance" : {
"text" : "13.7 mi",
"value" : 21984
},
"duration" : {
"text" : "25 mins",
"value" : 1473
},
"end_address" : "18 Hauppauge Rd, Commack, NY 11725, USA",
"end_location" : {
"lat" : 40.8286064,
"lng" : -73.2937987
},
"start_address" : "100 Nicolls Rd, Stony Brook, NY 11794, USA",
"start_location" : {
"lat" : 40.9003613,
"lng" : -73.1305969
},
"steps" : [
{
"distance" : {
"text" : "1.8 mi",
"value" : 2870
},
"duration" : {
"text" : "4 mins",
"value" : 258
},
"end_location" : {
"lat" : 40.87645940000001,
"lng" : -73.1181333
},
"html_instructions" : "Head \u003cb\u003esoutheast\u003c/b\u003e on \u003cb\u003eStony Brook Rd\u003c/b\u003e toward \u003cb\u003eDevelopment Drive\u003c/b\u003e",
"polyline" : {
"points" : "gjsxFfij}LLMb#[^UVK#?RGLCZIdB]d#KhBa#tAYhB]VG~A[jCg#pAWpA[zAc#xAa#vCw#jA]b#Qh#ULI|Ay#hAm#bDeB`CoAnAq#d#WTO|ByATO\\WbCcBbAu#hBqA^Un#e#VK`BiAjAq#tDaBx#_#nAg#pAi#lF}BpEmBpAk#nF{BrF_C`Bs#jBy#pAi#`Ac#nB{#"
},
"start_location" : {
"lat" : 40.9003613,
"lng" : -73.1305969
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "3.6 mi",
"value" : 5810
},
"duration" : {
"text" : "8 mins",
"value" : 456
},
"end_location" : {
"lat" : 40.8469852,
"lng" : -73.17149669999999
},
"html_instructions" : "Turn \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eNY-347 W\u003c/b\u003e/\u003cwbr/\u003e\u003cb\u003eSmithtown Bypass\u003c/b\u003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "{tnxFh{g}LhD~Gp#tCh#xA~#|B"
},
"start_location" : {
"lat" : 40.87645940000001,
"lng" : -73.1181333
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "4.6 mi",
"value" : 7390
},
"duration" : {
"text" : "7 mins",
"value" : 430
},
"end_location" : {
"lat" : 40.8288007,
"lng" : -73.2481703
},
"html_instructions" : "Continue straight to stay on \u003cb\u003eNY-347 W\u003c/b\u003e/\u003cwbr/\u003e\u003cb\u003eSmithtown Bypass\u003c/b\u003e",
"maneuver" : "straight",
"polyline" : {
"points" : "u|hxFzhr}Lj#fA|EVQv#sAjGs#|CS~#WbA"
},
"start_location" : {
"lat" : 40.8469852,
"lng" : -73.17149669999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "2.9 mi",
"value" : 4593
},
"duration" : {
"text" : "3 mins",
"value" : 177
},
"end_location" : {
"lat" : 40.8186598,
"lng" : -73.2952425
},
"html_instructions" : "Keep \u003cb\u003eleft\u003c/b\u003e to continue on \u003cb\u003eNorthern State Pkwy\u003c/b\u003e",
"maneuver" : "keep-left",
"polyline" : {
"points" : "_kexF`ha~L[pE`BI`D"
},
"start_location" : {
"lat" : 40.8288007,
"lng" : -73.2481703
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.1 mi",
"value" : 176
},
"duration" : {
"text" : "1 min",
"value" : 21
},
"end_location" : {
"lat" : 40.8195393,
"lng" : -73.29465789999999
},
"html_instructions" : "Take exit \u003cb\u003e43\u003c/b\u003e for \u003cb\u003eSuffolk County 4\u003c/b\u003e toward \u003cb\u003eCommack\u003c/b\u003e",
"maneuver" : "ramp-right",
"polyline" : {
"points" : "skcxFfnj~LMb#ADADADABABCBABCBGDABC#GBC#E?C#A?A?C?C?C?AAE?CACAECECGECCAECCACCICICOCUOkAIe#"
},
"start_location" : {
"lat" : 40.8186598,
"lng" : -73.2952425
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "299 ft",
"value" : 91
},
"duration" : {
"text" : "1 min",
"value" : 19
},
"end_location" : {
"lat" : 40.8197276,
"lng" : -73.29361089999999
},
"html_instructions" : "Keep \u003cb\u003eleft\u003c/b\u003e at the fork, follow signs for \u003cb\u003eCounty Road 4 N\u003c/b\u003e",
"maneuver" : "fork-left",
"polyline" : {
"points" : "cqcxFrjj~LQ{AOyAC["
},
"start_location" : {
"lat" : 40.8195393,
"lng" : -73.29465789999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.6 mi",
"value" : 997
},
"duration" : {
"text" : "2 mins",
"value" : 92
},
"end_location" : {
"lat" : 40.82857569999999,
"lng" : -73.29312469999999
},
"html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eCommack Rd\u003c/b\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "ircxF`dj~L?IAQKA{#CE?cAA_AC_AC_#CqEMw#Cw#A_BAYFWAQ?cCGsDK{FOsCIs#A[#Q?gA#E#k#BU#U#"
},
"start_location" : {
"lat" : 40.8197276,
"lng" : -73.29361089999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "187 ft",
"value" : 57
},
"duration" : {
"text" : "1 min",
"value" : 20
},
"end_location" : {
"lat" : 40.8286064,
"lng" : -73.2937987
},
"html_instructions" : "Turn \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eHauppauge Rd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDestination will be on the right\u003c/div\u003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "siexF~`j~LAp#An#Ad#"
},
"start_location" : {
"lat" : 40.82857569999999,
"lng" : -73.29312469999999
},
"travel_mode" : "DRIVING"
}
],
"traffic_speed_entry" : [],
"via_waypoint" : []
}
],
"overview_polyline" : {
"points" : "mt{wFnj~_M{#n#e#oAy#tA"
},
"summary" : "Northern State Pkwy and NY-347",
"warnings" : [],
"waypoint_order" : [ 0 ]
}
],
"status" : "OK"
}
I figured this out by using multiple nested "foreach" loops with several "if" tests. I'm sure it's not the most elegant but it works. The key (pun intended) was keeping track of all the index/keys for each nested foreach loop.

Remove mongo array if nested array is empty

i have got a mongo array , in which i want to remove the whole block , if the nested array of that block is empty
I have attached the array below
{
"_id" : ObjectId("5b17b991c440782b5a218cd1"),
"vendor_view_id" : 741733,
"product" : [
{
"id" : ObjectId("5b86546540c1c414543e4333"),
"vendor_user_id" : ObjectId("5b17b992c440782b5a218cd2"),
"product_type_id" : ObjectId("5ae8348b7ae0d9538e45ab46"),
"condition_id" : [ ],
"shipping_cost" : 100,
"date_added" : "2018-08-29-08-08-05",
"date_status_change" : "2018-08-29-08-08-05",
"status" : 0
},
{
"id" : ObjectId("5b8654ba40c1c4145d1f5473"),
"vendor_user_id" : ObjectId("5b17b992c440782b5a218cd2"),
"product_type_id" : ObjectId("5ae834b17ae0d9538e45ab48"),
"condition_id" : [ ],
"shipping_cost" : 100,
"date_added" : "2018-08-29-08-09-30",
"date_status_change" : "2018-08-29-08-09-30",
"status" : 0
},
{
"id" : ObjectId("5b8655a840c1c415080b0a33"),
"vendor_user_id" : ObjectId("5b17b992c440782b5a218cd2"),
"product_type_id" : ObjectId("5ae834a67ae0d9538e45ab47"),
"condition_id" : [
{
"_id" : ObjectId("5ae977da7ff1706f3b7dc47a"),
"status" : 0,
"date_added" : "2018-08-29-08-13-28"
}
],
"shipping_cost" : 100,
"date_added" : "2018-08-29-08-13-28",
"date_status_change" : "2018-08-29-08-13-28",
"status" : 0
}
]
}
I would like to delete the array block where product.condition_id is empty
So far i have tried this
$this->collection_name->collection->updateOne([
'_id' => $vendor_id,
],
[
'$unset' =>
[
'product.$.condition_id' =>
[
'$size'=>0,
]
]
])
EDIT 1:
db.collection_name.collection({_id : ObjectId('5b17b991c440782b5a218cd1'),
"product.condition_id.$":{ "$exists": false }},
{ "$unset": { "product.$": "" }});
still not working
db.collection_name.update(
{},
{$pull : {product : {condition_id : {$size : 0} }}},
{ multi: true } // multi : true will updates multiple documents that meet the query criteria
)
if condition_id array is empty pull the document.
Output:
{
"_id" : ObjectId("5b17b991c440782b5a218cd1"),
"vendor_view_id" : 741733,
"product" : [
{
"id" : ObjectId("5b8655a840c1c415080b0a33"),
"vendor_user_id" : ObjectId("5b17b992c440782b5a218cd2"),
"product_type_id" : ObjectId("5ae834a67ae0d9538e45ab47"),
"condition_id" : [
{
"_id" : ObjectId("5ae977da7ff1706f3b7dc47a"),
"status" : 0,
"date_added" : "2018-08-29-08-13-28"
}
],
"shipping_cost" : 100,
"date_added" : "2018-08-29-08-13-28",
"date_status_change" : "2018-08-29-08-13-28",
"status" : 0
}
]
}
You can transform your json to PHP array json_decode($json) so you'll be able to process it before it turn into json after.
json_encode($arr)
Note that you can check the depth of the array in your case to see if product.condition_id is empty.

mongodb update (insert array inside array) PHP

{
"_id" : "9127194b5bcebc099877d6192647412572892576",
"id_store" : "a907b33f4a3141ad2086815d841554b3f7dbe15a",
"created_on" : ISODate("2016-03-01T06:18:39.000Z"),
"product" : [
{
"_id" : "1967e1f0158a8589d49d9ffb8fe5236cf4d4482f",
"kategori" : "pakaian",
"created_on" : ISODate("2016-03-01T06:21:08.000Z"),
"jenis_barang_umum" : [
{
"_id" : "2dc12dec915f1ad013d8bc8610ed8d6cafea6995",
"jenis_barang_umum" : "baju",
"created_on" : ISODate("2016-03-01T07:52:26.000Z")
},
{
"_id" : "8d0fd34d9c96c82fd736772fd02eb01cf13d4561",
"jenis_barang_umum" : "celana",
"created_on" : ISODate("2016-03-01T07:52:32.000Z"),
"jenis_barang_spesifik" : [
{
"_id" : "1b14e9be168dc762713e35c453a91ceb653638ebb",
"jenis_barang_spesifik" : "jeans",
"created_on" : ISODate("2016-03-01T07:52:40.000Z")
}
]
},
{
"_id" : "1b14e9be168dc762713e35c453a91ceb65e638e8",
"jenis_barang_umum" : "jaket",
"created_on" : ISODate("2016-03-01T07:52:40.000Z"),
"jenis_barang_spesifik" : [
{
"_id" : "1b14e9be168dc762713e35c453a91ceb65e638ebb",
"jenis_barang_spesifik" : "jaket kulit",
"created_on" : ISODate("2016-03-01T07:52:40.000Z")
}
]
}
]
},
{
"_id" : "d839e4dd9ba895f7733a6713a653ae9fcf7b79b9",
"kategori" : "aksesoris",
"created_on" : ISODate("2016-03-01T06:21:13.000Z"),
"jenis_barang_umum" : [
{
"_id" : "4855847d579f31c17925218a5723764a8f0fc10a",
"jenis_barang_umum" : "topi",
"created_on" : ISODate("2016-03-01T07:42:36.000Z")
},
{
"_id" : "6a7a917f6e139552e3883594b73a67fb6fa4ad27",
"jenis_barang_umum" : "gelang",
"created_on" : ISODate("2016-03-01T07:47:22.000Z")
},
{
"_id" : "8bd3428f2ac106c5323af1defe125675f08afcf3",
"jenis_barang_umum" : "kalung",
"created_on" : ISODate("2016-03-01T07:47:28.000Z")
}
]
}
]
}
I have document like that, how syntax for inserting new array inside jenis_barang_spesifik (my data inside jenis_barang_spesifik added by robomongo so I dont know how the syntax is).
Here is my syntax for inserting new array into jenis_barang_spesifik but it didn't work. Hope you guys can help me :D
Thanks
$ar_jenis_barang_spesifik = array(
'_id' => hash('sha1', time() . $jenis_barang_spesifik),
'jenis_barang_spesifik' => $jenis_barang_spesifik,
'created_on' => new MongoDate()
);
$result = $jenis_barang_spesifik_collection->update(array('product.jenis_barang_umum._id.' => $jenis_barang_umum), array('$push' => array('product.$.jenis_barang_umum.$.jenis_barang_spesifik' =>$ar_jenis_barang_spesifik)));

Get one sub-documents that match a condition in subdocument field using mongodb

If I have a set of MongoDB documents like the following, what can I do to get a find() result that only returns the only one document
my document look like this
{
"_id" : ObjectId("549182eb1579f6340c000000"),
"cmp_userid" : "549129de471b083409000000",
"company_about" : "web development",
"company_address" : "kochin",
"company_location" : "kochin",
"company_name" : "innomind",
"company_state" : "Kerala",
"company_type" : "software",
"details" : [
{
"int_id" : ObjectId("549291dc1579f60c04123321"),
"title" : "php trainee",
"description" : "innomind",
"responsibilities" : "innomind resp",
"qualification" : "btech",
"sdate" : "1/1/2014",
"edate" : "2/1/2014",
"paid" : "Paid",
"salary" : "1111"
},
{
"int_id" : ObjectId("549291dc1579f60c04234300"),
"title" : "php trainee innomind",
"description" : "fsxgbhfgbh",
"responsibilities" : "gbhfgbhsdf",
"qualification" : "btech",
"sdate" : "1/1/2014",
"edate" : "2/1/2014",
"paid" : "unpaid",
"salary" : "1222"
},
{
"int_id" : ObjectId("549291dc1579f60c04221100"),
"title" : "web development",
"description" : "web development innomind",
"responsibilities" : "web development innomind",
"qualification" : "asdf",
"sdate" : "1/1/2014",
"edate" : "2/1/2014",
"paid" : "unpaid",
"salary" : "1222"
},
{
"int_id" : ObjectId("549291dc1579f60c04011110"),
"title" : "web development",
"description" : "fgfgfgfg",
"responsibilities" : "dddddd",
"qualification" : "asdf",
"sdate" : "1/1/2014",
"edate" : "2/1/2014",
"paid" : "unpaid",
"salary" : "1222"
},
{
"int_id" : ObjectId("549291dc1579f60c04000000"),
"title" : "seo",
"description" : "gdsxfgsdfg",
"responsibilities" : "fgsdf sdfgsdg.",
"qualification" : "btech",
"sdate" : "1/1/2014",
"edate" : "2/1/2014",
"paid" : "unpaid",
"salary" : "1111"
},
{
"int_id" : ObjectId("5492b2001579f60c04000002"),
"title" : "bpo",
"description" : "fvbhxdfvbhxdfgb",
"responsibilities" : "fgsdzfgsdf",
"qualification" : "btech",
"sdate" : "1/1/2014",
"edate" : "2/1/2014",
"paid" : "unpaid",
"salary" : "1111"
}
]
}
I want to get only one one document from details sub documents where _id= ObjectId("549182eb1579f6340c000000")
and int_id = ObjectId("549291dc1579f60c04123321")
expected results
_id:ObjectId("549182eb1579f6340c000000"),
"details" : [
{
"int_id" : ObjectId("549291dc1579f60c04123321"),
"title" : "php trainee",
"description" : "innomind",
"responsibilities" : "innomind resp",
"qualification" : "btech",
"sdate" : "1/1/2014",
"edate" : "2/1/2014",
"paid" : "Paid",
"salary" : "1111"
}]
but i can't get any results please help me
Since it is an embedded document and u want only the embedded document which matches the query u have to use $unwind
db.can.aggregate({$unwind:"$details"},{$match:{_id: ObjectId("549182eb1579f6340c000000"),"details.int_id" : ObjectId("549291dc1579f60c04123321")}})

Categories