mongodb-php update $pull - php

There is a collection "emailDeliveryActive":
{
"_id": ObjectId("4f1950f0e902edfc3e000001"),
"coupons": {
"4f1950b7e902edf23e000001": {
"_id": ObjectId("4f1950b7e902edf23e000001"),
"couponID": ObjectId("4f15c7d8e902edb667000000")
},
"4f1950bfe902ed843f000000": {
"_id": ObjectId("4f1950bfe902ed843f000000"),
"couponID": ObjectId("4f171f33e902ed4f4f000002")
}
},
"recipients": [
{
"email": "test1#gmail.com",
"get": "?auth=ZG1pdHJ5LnZvbG9zbmloaW5AZ21haWwuY29tfDA5OGY2YmNkNDYyMWQzNzNjYWRlNGU4MzI2MjdiNGY2"
},
{
"email": "test2#gmail.com",
"get": "?auth=ZGpyb3VibGVAZ21haWwuY29tfDA5OGY2YmNkNDYyMWQzNzNjYWRlNGU4MzI2MjdiNGY2"
},
{
"email": "test3#gmail.com",
"get": "?auth=a2FsaWJyb3YxQGdtYWlsLmNvbXwwOThmNmJjZDQ2MjFkMzczY2FkZTRlODMyNjI3YjRmNg=="
}
],
"title": "test"
}
Must be from an array of "recipients" to remove an item from a given email.
Doing so:
$result = $mongoDB->emailDeliveryActive->update(
array('_id' => $emailDelivery['_id']),
array(
'$pull'=>array(
'recipients.$.email' => 'test1#gmail.com'
)
)
);
$result in getting TRUE, but the collection does not change. I'm doing something wrong?

You don't need the $ operator- it is for the "position of the matched array item in the query"- doesn't make sense to use here. Try as:
$result = $mongoDB->emailDeliveryActive->update(
array('_id' => $emailDelivery['_id']),
array(
'$pull'=> array('recipients' => array('email' => 'test1#gmail.com'))
)
);

Related

Include variable into array if not empty inside foreach

Include the name variable if the value is not empty inside the foreach. I tried this method code below but it include outside the array. I want to include within the array.
Sample Code:
$load_array = array();
foreach($stmt->fetchAll() as $row){
$load_array[] = array(
'name' => $row['name'],
'start_location' => array(
'address' => $row['start_address'],
'coords' => array (
'lat' => floatval($row['start_lat']),
'lng' => floatval($row['start_lng']),
),
),
);
if(!empty($row['shift_start'])) { $load_array['shift_start'] = $row['shift_start'];}
if(!empty($row['shift_end'])) { $load_array['shift_end'] = $row['shift_end'];}
}
return json_encode($load_array);
Output code above:
{
"0": {
"name": "Ramon Macger",
"start_location": {
"address": "76 Spilman Street GORSGOCH SA40 8ZB",
"coords": {
"lat": 42.45188,
"lng": -83.12829
}
}
},
"1": {
"name": "Roberto",
"start_location": {
"address": "76 Spilman Street GORSGOCH SA40 8ZB",
"coords": {
"lat": 42.45188,
"lng": -83.12829
}
}
},
"shift_start": "14:00",
"shift_end": "17:00"
}
The output should be like this:
{
"0": {
"name": "Ramon Macger",
"start_location": {
"address": "76 Spilman Street GORSGOCH SA40 8ZB",
"coords": {
"lat": 42.45188,
"lng": -83.12829
}
},
"shift_start": "14:00",
"shift_end": "17:00"
},
"1": {
"name": "Roberto",
"start_location": {
"address": "76 Spilman Street GORSGOCH SA40 8ZB",
"coords": {
"lat": 42.45188,
"lng": -83.12829
}
},
"shift_start": "14:00",
"shift_end": "17:00"
},
}
The shift_start and shift_end should be within the array not outside. My code is working in 1 array only and not on the foreach. However, it doesn't working within for each.
You need to add shift_start and shift_end values into the data array before you push it into $load_array. Something like this:
foreach($stmt->fetchAll() as $row){
$data = array(
'name' => $row['name'],
'start_location' => array(
'address' => $row['start_address'],
'coords' => array (
'lat' => floatval($row['start_lat']),
'lng' => floatval($row['start_lng']),
),
),
);
if(!empty($row['shift_start'])) {
$data['shift_start'] = $row['shift_start'];
}
if(!empty($row['shift_end'])) {
$data['shift_end'] = $row['shift_end'];
}
$load_array[] = $data;
}

How do we sum individual array elements in MongoDB aggregation query?

Document :
{
"version": "1.0.0",
"actor": {
"objectType": "Agent",
"name": "Test user",
"account": {
"homePage": "http://testing.com/",
"name": "67"
}
},
"verb": {
"id": "http://adlnet.gov/expapi/verbs/completed",
"display": {
"en-US": "completed"
}
},
"object": {
"objectType": "Activity",
"id": "http://localhost/action?id=cji",
"definition": {
"type": "http://adlnet.gov/expapi/activities/lesson",
"name": {
"en-US": "ps3"
},
"description": {
"en-US": "ps3"
}
}
},
"timestamp": "2016-10-25T11:21:25.917Z",
"context": {
"extensions": {
"http://localhost/eventinfo": {
"sessionId": "1477393533327",
"starttm": "1477394351210",
"eventtm": "1477394485917",
"course": "cji"
}
},
"contextActivities": {
"parent": [
{
"objectType": "Activity",
"id": "http://localhost/id=cji"
}
]
}
},
"result": {
"duration": "PT2M14.71S",
"score": {
"raw": 6,
"max": 21
}
},
"authority": {
"objectType": "Agent",
"name": "New Client",
"mbox": "mailto:hello#lhd.net"
},
"stored": "2016-10-25T11:20:29.666700+00:00",
"id": "c7039783-371f-4f59-a665-65a9d09a2b7f"
}
We've got this PHP + MongoDB aggregation query:
$condition = array(
array(
'$match' => array(
'client_id' => $CFG->mongo_clientid,
'statement.actor.account.name' => array('$in'=> array('67','192','213')),
'statement.verb.id' => 'http://adlnet.gov/expapi/verbs/completed',
'statement.object.id' => 'http://localhost/action?id=cji'
)),
array(
'$group' => array(
'_id' => '$statement.actor.account.name' ,
//'totalpoints' =>array( '$sum' => array('$last' => '$statement.result.score.raw'))
'laststatement' => array('$last' => '$statement.result.score.raw'),
//'sumtest' => array('$add' => ['$laststatement'])
)
)
);
$cursor = $collection->aggregate($condition);
echo "";
print_r($cursor);
echo "";
which returns this result:
Array
(
[result] => Array
(
[0] => Array
(
[_id] => 192
[laststatement] => MongoInt64 Object
(
[value] => 4
)
)
[1] => Array
(
[_id] => 67
[laststatement] => MongoInt64 Object
(
[value] => 6
)
)
)
[ok] => 1
)
How do we sum [laststatement].[value] of these individual array elements in MongoDB aggregation query?
[laststatement] => MongoInt64 Object
(
[value] => values goes here
)
Also, how do we use $last and $sum together in MongoDB aggregation query?
In my result there are 2 raw scores(last statement) for 2 different id (192,67). I want to sum this scores like 4 + 6 = 10 for all multiple id's but want only the last scores from the last statement. I am unable to use $last and $sum on the line. Please check
Looks like all you want is a single group. So the grouping id should be null. You may want to add a sort if you care for what last record should be. Not tested.
array(
'$group' => array(
'_id' => null ,
'totalpoints' => array( '$sum' => '$statement.result.score.raw')
'laststatement' => array('$last' => '$statement.result.score.raw')
)
)
Here is the mongo shell version.
aggregate([
{
$match :{
"actor.account.name":{$in:["67","192","213"]},
"verb.id":{$eq:"http://adlnet.gov/expapi/verbs/completed"},
"object.id":{$eq:"http://localhost/action?id=cji"}
}
},
{
$group: {
"_id": null,
"totalpoints" : {$sum:"$result.score.raw"},
"laststatement" :{$last:"$result.score.raw"}
}
}
])
Output:
{ "_id" : null, "totalpoints" : 10, "laststatement" : 4 }
Update Changed to include the sum for the last statement from each group. The first grouping is by actor name and returns the last statement from each group. The second grouping sums all the last statement.
aggregate([{
$match: {
"actor.account.name": {
$in: ["67", "192", "213"]
},
"verb.id": {
$eq: "http://adlnet.gov/expapi/verbs/completed"
},
"object.id": {
$eq: "http://localhost/action?id=cji"
}
}
}, {
$group: {
"_id": "$actor.account.name",
"laststatement": {
$last: "$result.score.raw"
}
}
}, {
$group: {
"_id": null,
"totalpoints": {
$sum: "$laststatement"
},
}
}])

Add array elements in an array position

How can I add an array to array position:
Something like a:
<?php
$newArr = array('email' => array("id" => "5678", "token" => "fghjk"));
$arr = array(
"auth"=>
array(
'users'=>
array(
'id' =>"456yhjoiu",
'token' => "asdfghjkrtyui678"
)
)
);
somefunction($arr['auth'], $newArr);
I've tried array_push() but it added zero (0) before 'email' instead.~
I'm doing this to get a json output, something like this:
}
"auth": {
"users": {
"id": "456yhjoiu",
"token": "asdfghjkrtyui678"
},
"email": {
"id": "5678",
"token": "fghjk"
}
}
}
but I have this output:
{
"auth": {
"users": {
"id": "456yhjoiu",
"token": "asdfghjkrtyui678"
},
"0": {
"email": {
"id": "5678",
"token": "fghjk"
}
}
}
$data = ['auth' => array_merge($arr['auth'], $newArr)];
or old array notation <= PHP5.3
$data = array('auth' => array_merge($arr['auth'], $newArr));

Multi update with $set and php Mongo

I'm trying to update multiple items in a collection using the update() function with multi=true param on mongolab platform. The problem is only the first item in the collection is updated.
The collection:
{
"_id": {
"$oid": "5454b446e4b02a012c939a0a"
},
"value": "1234",
"name": "first"
},
{
"_id": {
"$oid": "5454b446e4b02a012c939a0a"
},
"value": "1234",
"name": "second"
}
Script code:
$db->collection->update(
array('value' => '1234'),
array('$set' => array(
'name' => 'example name',
)),
array('multi' => true)
);
Result:
{
"_id": {
"$oid": "5454b446e4b02a012c939a0a"
},
"value": "1234",
"name": "example name"
},
{
"_id": {
"$oid": "5454b446e4b02a012c939a0a"
},
"value": "1234",
"name": "second"
}
The update() function accepts only three arrays as arguments.
Hi Yogesh,
I think your query should be:
db.collectionName.update({"value":"1234"},{$set:{"name":"example name"}},{'upsert':true, 'multiple': true});
The corresponding PHP codes would be:
<?php
$m = new MongoClient(); //connects to local mongo
$db_name = 'ri'; //replace database name you want to work with
$collection_name = 'brand_graph'; //replace collection name you want to work with
$select = $m->selectDB($db_name)->selectCollection($collection_name);
$where_array = array(
'value' => 1234
);
$update_data = array(
'$set' => array(
'name' => 'example name'
)
);
$options = array(
'upsert' => true,
'multiple' => true
);
$select->update($where_array, $update_data, $options);
?>
I hope this is what your were looking for.
I write below mongo query which update multiple documents but , I don't know how to convert this in PHP code but for reference you should use this query
db.collectionName.update({"value":"1234"},{$set:{"name":"example name"}},true,true)

I can not get data to aggregate It. PHP + mongoDB

It works like I need:
$out = $collection->aggregate(
array(
'$match' => array('type' => 'chair')
),
array(
'$project' => array(
'chairtype' => 1,
'mijczjeqeo'=>1
)
),
array(
'$group' => array(
'_id' => '$chairtype',
'MIDDLE_mijczjeqeo' => array('$avg' => '$mijczjeqeo'),
'SUMMA__mijczjeqeo' => array('$sum' => '$mijczjeqeo')
)
)
);
my_dump($out);
But i need to get true data for aggregation from array in the same documents: versions[0][content][mijczjeqeo]
Please correct my script. It does not work:
$out = $collection->aggregate(
array(
'$match' => array('type' => 'chair')
),
array(
'$project' => array(
'chairtype' => 1,
'versions.0.content.mijczjeqeo'=>1
)
),
array(
'$group' => array(
'_id' => '$chairtype',
'MIDDLEmijczjeqeo' => array('$avg' => '$versions.0.content.mijczjeqeo'),
'SUMMAmijczjeqeo' => array('$sum' => '$versions[0]["content"]["mijczjeqeo"]')
)
)
);
no one method does not work:
'MIDDLEmijczjeqeo' => array('$avg' => '$versions.0.content.mijczjeqeo')
'SUMMAmijczjeqeo' => array('$sum' => '$versions[0]["content"]["mijczjeqeo"]')
I think the problem near .0.
I try to do it in mongo console...
db.documents.aggregate({$match:{'type':'chair'}},{$project:{'chairtype': 1, 'mijczjeqeo':1}},{$group:{'_id':'$chairtype','MID':{$avg:'$mijczjeqeo'}}})
{
"result" : [
{
"_id" : "T",
"MID" : 6.615384615384615
},
{
"_id" : "G",
"MID" : 8.310344827586206
},
{
"_id" : "E",
"MID" : 6.9523809523809526
}
],
"ok" : 1
}
db.documents.aggregate({$match:{'type':'chair'}},{$project:{'chairtype': 1, 'versions.0.content.mijczjeqeo':1}},{$group:{'_id':'$chairtype','MID':{$avg:'$versions.0.content.mijczjeqeo'}}})
{
"result" : [
{
"_id" : "T",
"MID" : 0
},
{
"_id" : "G",
"MID" : 0
},
{
"_id" : "E",
"MID" : 0
}
],
"ok" : 1
}
Well you cannot project like that in the aggregation pipeline. If you want to act on array elements within an aggregation statement you first need to $unwind the array and then either $match the required element(s) or as in your case choose the $first item using an additional $group stage.
Your question does not show the structure of a document so I'll just use a sample, as my "chairs" collection:
{
"_id": 1,
"type": "chair",
"chairtype": "A",
"versions": [
{
"revision": 1,
"content": {
"name": "ABC",
"value": 10
}
},
{
"revision": 2,
"content": {
"name": "BBB",
"value": 15
}
}
]
}
{
"_id": 2,
"type": "chair",
"chairtype": "A",
"versions": [
{
"revision": 1,
"content": {
"name": "CCC",
"value": 20
}
},
{
"revision": 2,
"content": {
"name": "BAB",
"value": 12
}
}
]
}
Minimal, but enough to get the point. Now the aggregate statement:
db.chairs.aggregate([
// Normal query matching, which is good
{ "$match": { "type": "chair" } },
// Unwind the array to de-normalize
{ "$unwind": "$versions" },
// Group by the document in order to get the "first" array element
{ "$group": {
"_id": "$_id",
"chairtype": { "$first": "$chairtype" },
"versions": { "$first": "$versions" }
}},
// Then group by "chairtype" to get your average values
{ "$group": {
"_id": "$chairtype",
"MID": {"$avg": "$versions.content.value"}
}}
])
Of course if your actual document has nested arrays then you will be "unwinding" and "matching" the required elements. But that is the general process of "narrowing down" the array contents to the elements you need.

Categories