Remove mongo array if nested array is empty - php

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.

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
}

fetch nested array and update fields simultaneously in mongodb PHP

I have the following value in mongodb's collection . Now i want to fetch the single field inside the array where status is 1 and transferred is 0 . But i also want to set transferred to 1 in the same query so that i don't have to write multiple queries .
Following is my collection
{
"_id" : ObjectId("5b5c268af93afb77f9d904db"),
"vendor_id" : ObjectId("5b17b991c440782b5a218cd1"),
"single" : [
{
"product_id" : ObjectId("5af5619d977f8723188a8f81"),
"product_type_id" : ObjectId("5ae834807ae0d9538e45ab45"),
"condition_id" : ObjectId("5ae978587ff1706f3b7dc47e"),
"selling_price" : 100,
"quantity" : "27",
"shipping" : "44",
"date_added" : "2018-07-28-08-17-14",
"date_live" : 0,
"status" : 1,
"flag" : 0,
"transferred" : 0
},
{
"product_id" : ObjectId("5af2bd44a003533a8abf4e56"),
"product_type_id" : ObjectId("5ae834807ae0d9538e45ab45"),
"condition_id" : ObjectId("5ae978187ff1706f3b7dc47c"),
"selling_price" : 200,
"quantity" : 403,
"shipping" : 702,
"date_added" : "2018-08-21-10-39-47",
"date_live" : 1,
"status" : 1,
"flag" : 0,
"transferred" : 0
},
{
"product_id" : ObjectId("5af2bd44a003533a8abf4e56"),
"product_type_id" : ObjectId("5ae834807ae0d9538e45ab45"),
"condition_id" : ObjectId("5ae977da7ff1706f3b7dc47a"),
"selling_price" : 100,
"quantity" : 809,
"shipping" : 11,
"date_added" : "2018-09-04-13-44-10",
"date_live" : 0,
"status" : 0,
"flag" : 0,
"transferred" : 0
}
]
}
So far i have written this
$pipeline=[
[
'$match'=>
[
'vendor_id'=>$vendor_id,
'single.status'=>1,
'single.transferred'=>0
]
],
[
'$project'=>
[
'single'=>1
]
]
];
But i am not able to set the transferred value to 1

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)));

Mongodb multi array to individual array on search

My aim is to create inner array to main array when search for userid as 1
Below is my data
{ "_id" : 2,
"name" : "test1",
"data" :[{"_id" : "1","file" : "nic", "userid" : [1,2 ]},
{"_id" : "2","file" : "nic1","userid" : [1 ] },
{"_id" : 3,"file" : "nick2","userid" : [1,2 ]}
]},
{ "_id" : 3,
"name" : "test2",
"data" : [{"_id" : "1","file" : "nic","userid" : [1,2 ] },
{"_id" : "2","file" : "nic1", "userid" : [3,2 ] }
]}
need to get out put as
{"_id" : 1,"file" : "nic", "userid" : [1,2 ],"main_name" : "test1","main_id" : 2},
{"_id" : 2,"file" : "nic1","userid" : [1 ] ,"main_name" : "test1","main_id" : 2 },
{"_id" : 3,"file" : "nick2","userid" : [1,2 ],"main_name" : "test2","main_id" : 3},
{"_id" : 1,"file" : "nic","userid" : [1,2 ] ,"main_name" : "test2" ,"main_id" : 3}
Basically the same answer to your last question, but without the $group to reconstruct the array and use a $project instead to restructure the document from the already de-normalized array elements.
$collection->aggregate(array(
array( '$match' => array( "data.userid" => 1 )),
array( '$unwind' => '$data' ),
array( '$match' => array( 'data.userid' => 1 )),
array(
'$project' => array(
'_id' => '$data._id',
'nic' => '$data.nic',
'user_id' => '$data.user_id',
'main_name' => '$name',
'main_id' => '$_id'
)
)
))

Insert data in nested array in mongodb [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MongoDB updating fields in nested array
I have data like:
{
"_id" : ObjectId("4f855061dd53351011000b42"),
"act_mgr" : [{ "sales" : {"agent" : ["rohan#walkover.in" ], "last_interacted" : "rohan#walkover.in" } } ],
"email" : "aman#asasas.com", "name" : "Aman",
"sales" : [{"sno" : 1, "message" : "description","status" : "open"},{"sno" : 12,"message" : "assad","status" :"open"}]
}
I want to add new agent and update last_interacted in act_mgr:sales something like that
"act_mgr" : [{ "sales" : {"agent" : ["rohan#walkover.in","abc#walkover.in" ],
"last_interacted" : "abc#walkover.in" } } ]
Also if I add new act_mgr like developer then it would be like
"act_mgr" : [{ "sales" : {"agent" : ["rohan#walkover.in","abc#walkover.in" ], "last_interacted" : "abc#walkover.in" } },
{ "developer" : {"agent" : ["newdeveloper#walkover.in" ], "last_interacted" : "newdeveloper#walkover.in" } } ]
I dont know how to add these fields
You can update the embedded "sales" document inside of the "act_mgr" array with the following update statement:
> db.sales.update({"act_mgr.sales.last_interacted":"rohan#walkover.in"}, {$push:{"act_mgr.$.sales.agent":"abc#walkover.in"}, $set:{"act_mgr.$.sales.last_interacted":"abc#walkover.in"}})
> db.sales.find().pretty()
{
"_id" : ObjectId("4f855061dd53351011000b42"),
"act_mgr" : [
{
"sales" : {
"agent" : [
"rohan#walkover.in",
"abc#walkover.in"
],
"last_interacted" : "abc#walkover.in"
}
}
],
"email" : "aman#asasas.com",
"name" : "Aman",
"sales" : [
{
"sno" : 1,
"message" : "description",
"status" : "open"
},
{
"sno" : 12,
"message" : "assad",
"status" : "open"
}
]
}
>
You can add the embedded document containing the "developer" information to the array like so:
> db.sales.update({"_id" : ObjectId("4f855061dd53351011000b42")}, {$push:{"act_mgr":{ "developer" : {"agent" : ["newdeveloper#walkover.in" ], "last_interacted" : "newdeveloper#walkover.in" } }}})
> db.sales.find().pretty()
{
"_id" : ObjectId("4f855061dd53351011000b42"),
"act_mgr" : [
{
"sales" : {
"agent" : [
"rohan#walkover.in",
"abc#walkover.in"
],
"last_interacted" : "abc#walkover.in"
}
},
{
"developer" : {
"agent" : [
"newdeveloper#walkover.in"
],
"last_interacted" : "newdeveloper#walkover.in"
}
}
],
"email" : "aman#asasas.com",
"name" : "Aman",
"sales" : [
{
"sno" : 1,
"message" : "description",
"status" : "open"
},
{
"sno" : 12,
"message" : "assad",
"status" : "open"
}
]
}
>
The documentation on the $push and $set modifiers may be found in the "Updating" documentation:
http://www.mongodb.org/display/DOCS/Updating
More information on creating and updating embedded documents with Mongo db may be found in the documentation titled "Dot Notation (Reaching into Objects)"
http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29
Information on updating embedded documents using the "$" positional operator may be found in the "The $ positional operator" section of the "Updating" documentation.
http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator
A word of caution: It is generally more common to have embedded documents all match the same structure, so that individual embedded documents may be referenced more easily. Your "sales" array is a good example of this; each embedded document contains the same keys, "sno", "message", and"status"
However, the embedded documents inside your "act_mgr" array contain different keys; the first contains "sales", and the second contains "developer". Instead, maybe consider the following structure:
"act_mgr" : [
{
"title" : "sales",
"agent" : [
"rohan#walkover.in",
"abc#walkover.in"
],
"last_interacted" : "abc#walkover.in"
},
{
"title": "developer",
"agent" : [
"newdeveloper#walkover.in"
],
"last_interacted" : "newdeveloper#walkover.in"
}
]
Now, each embedded documents contain the same keys, "title", "agent", and "last_interacted".
You could update sub-documents with the following command.
> db.sales.update({"act_mgr.title":"sales"}, {$push:{"act_mgr.$.agent":"abc#walkover.in"}, $set:{"act_mgr.$.last_interacted":"abc#walkover.in"}})
Hopefully this will allow you to make the updates that you need to, and perhaps give you some food for thought regarding schema design. Good luck!

Categories