PHP \MongoDB\Driver\BulkWrite can't delete _id.$oid Object - mongodb

I have a script removing Mongo records on PHP7 \MongoDB\Driver as this
$db = new \MongoDB\Driver\Manager(".......");
$bulk = new \MongoDB\Driver\BulkWrite;
$bulk->delete(["_id":"5887e6fda69ca3797f74a4fd"], ['limit' => 1]);
$writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $db->executeBulkWrite('theplace.here', $bulk, $writeConcern);
I also tried
...
$bulk->delete(["_id.$oid":"5887e6fda69ca3797f74a4fd"], ['limit' => 1]);
...
This works for any field I send, it does delete... it just doesn't work when I want to remove an _id _id.$oid, like if it was ignoring without throwing errors.
Any help really appreciated.

You should use a MongoDB\BSON\ObjectID when matching an _id with an ObjectId value. As strings and ObjectIds are not directly comparable:
$bulk->delete(
["_id" => new MongoDB\BSON\ObjectID("5887e6fda69ca3797f74a4fd")],
['limit' => 1]
);

Related

Yii2 MongoDB delete single row from document

I try this two way to delete but unable to do so:--
Try one:-
$query = new Query();
$query->delete()->from('table_name')->where(['disease_id' => 'A0PO919Q-12', 'status' => 1]);
Try Two:-
$collection = Yii::$app->mongodb->getCollection('table_name');
$collection->delete(['disease_id' => 'A0PO919Q-12']);
Can anyone try to delete data from mongo DB in Yii2 Framewrok
I find the solution
$collection = Yii::$app->mongodb->getCollection('table_name');
$collection->remove(['disease_id' => 'A0PO919Q-12']);

Insert query using foreach

I'm trying to generate a statement sheet. For that, I try to insert data into Mysql using foreach. It works great. But I try to escape the insert query if any duplicate row found in database. I do not want to escape the whole process, but it should escape if a duplicate row found and continue with the next insert query.
$gps = $this->Webmastermodel->acquireStatementDetail();
foreach($gps as $statement){
$insertdata = array(
'product_invoice_id' => $statement['InvoiceNo'],
'PUC' => $statement['productPUC'],
'productQTY' => $statement['productQty'],
'product_sub_total' => $statement['productSubTotal'],
'commissionGain_on_SubTotal' => $statement['commisionChargedOnProductSubTotal']
);
$this->db->insert('partner_business', $insertdata);
}
I would use insert_batch if i were you:
$insertdata = array();
foreach($gps as $statement)
{
$insertdata[] = array(
'product_invoice_id' => $statement['InvoiceNo'],
'PUC' => $statement['productPUC'],
'productQTY' => $statement['productQty'],
'product_sub_total' => $statement['productSubTotal'],
'commissionGain_on_SubTotal' => $statement['commisionChargedOnProductSubTotal']
);
}
$this->db->insert_batch('partner_business', $insertdata);
But since your question is about skipping if a row already exits, you can modify your query and use replace instead:
$this->db->replace('partner_business', $insertdata);
Keeping in mind that its all about providing the right primary_key in your $data array, if there is and existing id it will update its data else it will insert a new row.

Return latest 2 entries from a mongo database?

I have a database recording the unix time, latitude and longitude of a received message from a GPS unit. I need a way to select the latest two entries from this database to work with in php.
Example database entry:
[3] => stdClass Object ( [_id] => MongoDB\BSON\ObjectId Object ( [oid] => 5c7fe3fc1c37210bf96e8182 ) [Latitude] => 53.385360717773 [Longitude] => -6.6032276153564 [Time] => 1551885285 ) )
So far I only needed one and I got it by searching for the largest timestamp. Is there a way to simply return the latest two entries in mongo like in mySQL? I've noticed 'limit' returns the first entries not the last:
$options=['limit' => 2];
$filter=[];
//$cursor = $collection->find($filter, $options);
$cursor = $collection->find($filter);
$largest = 0;
foreach($cursor as $document){
if ($largest < $document["Time"]) {
$largest = $document["Time"];
$longitude = $document["Longitude"];
$latitude = $document["Latitude"];
}
Thanks for any help.
edit: I've found this question How to get the last N records in mongodb?. The mongo version is different but the principle seems sound: use 'sort' then 'limit'. The syntax is quite different thought how could I combine the two in this version?
"db.foo.find().sort({_id:1}).limit(50);"
$options=['limit' => 2];
$filter=[];
$cursor = $collection->find($filter, $options);
Keep extending the query as follows:
$cursor = $collection->find($filter)->sort(array('_id'=>-1))->limit(2)
Got it to work with:
$options = ['sort' => ['Time' => -1], 'limit' => 2];
$filter=[];
$cursor = $collection->find($filter, $options);

MongoDB & PHP - Returning a count of a nested array

Imagine I have a MonogDB collection containing documents as follows:
{name: 'Some Name', components: {ARRAY OF ITEMS}}
How can I return the name and the count of items in components?
Do I have to use a map/reduce?
I am using PHP's Mongo extension.
EDIT: Snippet of current code in PHP (working) but I just want count of the components
$fields = array(
'name', 'components'
);
$cursor = $this->collection->find(array(), $fields);
$cursor->sort(array('created_ts' => -1));
if (empty($cursor) == true) {
return array();
} else {
return iterator_to_array($cursor);
}
Thanks,
Jim
You could use map-reduce or you could use a simple group query as follows. Since I am assuming that your name property is a unique key, this should work even though it isn't a reason that you'd normally use the group function:
db.test.group({
key: { name:true },
reduce: function(obj,prev) {
var count = 0;
for(k in obj.components)
count++;
prev.count = count;
},
initial: { count: 0}
});
You mentioned that you have an array of components, but it appears that you are storing components as an object {} and not and array []. That is why I had to add the loop in the reduce function, to count all of the properties of the components object. If it were actually an array then you could simply use the .length property.
In PHP it would look something like this (from the Manual):
$keys = array('name' => 1);
$initial = array('count' => 0);
$reduce =<<<JS
function(obj,prev) {
var count = 0;
for(k in obj.components)
count++;
prev.count = count;
},
JS;
$m = new Mongo();
$db = $m->selectDB('Database');
$coll = $db->selectCollection('Collection');
$data = $coll->group($keys, $initial, $reduce);
Finally, I would strongly suggest that if you are trying to access the count of your components on a regular basis that you store the count as an additional property of the document and update it whenever it changes. If you are attempting to write queries that filter based on this count then you will also be able to add an index on that components property.
You could use db.eval() and write the calculation in JavaScript.
Jim-
These are two separate operations; Unless you want to leverage PHP's count on the results you get which you would then do something like:
$m = new Mongo();
$db = $m->selectDB('yourDB');
$collection = $db->selectCollection('MyCollection');
$cursor = $collection->find(array(), array("name"=>1, "components"=>1));
foreach($cursor as $key){
echo($key['name'].' components: '.count($key['components']);
}
Ran across this today, If your using the new driver with aggregate you can do this in php, ( given this schema )
{name: 'Some Name', components: {ARRAY OF ITEMS}}
In PHP:
$collection = (new Client())->db->my_collection;
$collection->aggregate([
'$match' => ['name' => 'Some Name'],
'$group' => [
'_id' => null,
'total'=> ['$sum' => "\$components"]
]
]);
The trick here with PHP is to escape the $ dollar sign, this is basically what the mongo documentation says when using size or sum
https://docs.mongodb.com/manual/reference/operator/aggregation/size/
https://docs.mongodb.com/manual/reference/operator/aggregation/sum/
The problem I had is mongo puts fields in as "$field" and PHP doesn't like that at all because of the way it does variable interpolation. However, once you escape the $, it works fine.
I think for this particular case you'd need to do something similar but with $project instead of $group Like this
$collection = (new Client())->db->my_collection;
$collection->aggregate([
'$match' => ['name' => 'Some Name'],
'$project' => [
'name' => "\$name",
'total'=> ['$sum' => "\$components"]
]
]);
This is an old question but seeing as there is no answer picked, I'll just leave this here.

How to get affected number of rows using mongodb ,php driver

I have two problem : How can I get affected rows by php mongodb driver ,and how about the last insert id ? thanks .
You can get number of results right from the cursor using count function:
$collection->find()->count();
You can even get number of all records in collection using:
$collection->count();
Using insert method, _id is added to input array automatically.
$a = array('x' => 1);
$collection->insert($a,array('safe'=>true));
var_dump($a);
array(2) {
["x"]=>
int(1)
["_id"]=>
object(MongoId)#4 (0) {
}
}
I don't believe that there is any type of affected_rows() method at your disposal with mongodb. As for the last insert _id You can generate them in your application code and include them in your insert, so there's really no need for any mysql like insert_id() method.
$id = new MongoId();
$collection->insert(array('
'_id' => $id,
'username' => 'username',
'email' => 'johndoe#gmail.com'
'));
Now you can use the object stored in $id however you wish.
Maybe MongoDB::lastError is what you are looking for:
(http://php.net/manual/en/mongodb.lasterror.php)
It calls the getLastError command:
(http://www.mongodb.org/display/DOCS/getLastError+Command)
which returns, among other things:
n - if an update was done, this is the number of documents updated.
For number of affected rows:
$status = $collection->update( $criteria, array( '$set' => $data ) );
$status['n']; // 'n' is the number of affected rows
If you have the output of your action, you can call relative function:
// $m hold mongo library object
$output = $m->myCollection->updateOne([
'_id' => myMongoCreateID('56ce2e90c9c037dba19c3ce1')], [
'$set' => ['column' => 'value']
]);
// get number of modified records
$count = $output->getModifiedCount();
$output is of of type MongoDB\UpdateResult. Relatively check following files to figure out the best function to find inserted, deleted, matched or whatever result you need:
https://github.com/mongodb/mongo-php-library/blob/master/src/InsertManyResult.php
https://github.com/mongodb/mongo-php-library/blob/master/src/DeleteResult.php
https://github.com/mongodb/mongo-php-library/blob/master/src/InsertOneResult.php
https://github.com/mongodb/mongo-php-library/blob/master/src/UpdateResult.php