Updating sharding document with ISODate in PHP

62 views
Skip to first unread message

Onyon

unread,
Sep 19, 2012, 9:05:38 AM9/19/12
to mongod...@googlegroups.com
Hi, i have some sharding enable collection called blog with name and saved_at (ISODate) for its shard keys. when updating comments that embedding in its documents, ISODate need to be called and also name, for its shard keys. in the shell, the command look like:

mongos> db.blog.update({'name':'myname','saved_at':ISODate('2012-09-19T03:47:44.602Z')}, {$set: {'comment':'its my first comment for sample.'}};

my question is: How to implement it to PHP code?. i still confuse about an ISODate issue in mongoDB - PHP. i've tried to searching it at Google but not find the best result to solve that problem.
Thank you.

Gianfranco

unread,
Sep 19, 2012, 12:57:15 PM9/19/12
to mongod...@googlegroups.com
Here's an example:
  1. // Connect
  2. $m = new Mongo();
  3.  
  4. // Select a database
  5. $db = $m->test;
  6.  
  7. // Select a collection (analogous to a relational database's table)
  8. $collection = $db->blog;
  9.  
  10. // Description of the objects to update.
  11. $criteria = array("name" => "myname", "saved_at" => $date);
  12.  
  13. // The object with which to update the matching records.
  14. $newdata = array('$set' => array("comment" => "its my first comment for sample."));
  15.  
  16. // Do the update
  17. $collection->update($criteria, $newdata);
  18.  
  19. // Iterate through the results
  20. foreach ($cursor as $obj) {
  21.     echo $obj["name"] . " " . date('Y-M-d h:i:s', $obj["saved_at"]->sec) . " " .  $obj["comment"] . "\n";
  22. }
And if you look in the shell you see an ISODate():

  1. > db.cartoons.find()
  2. { "_id": ObjectId("5059f8443e20d65344000000"), "name": "myname", "comment": "its my first comment for sample.", "saved_at": ISODate("20120919T02:47:44Z") }
Message has been deleted

Onyon

unread,
Sep 19, 2012, 7:29:41 PM9/19/12
to mongod...@googlegroups.com
Thanks Gianfranco, but i still have a trouble in using ISODate value in document which will updated.
How about $date in line 11 if i throw the saved_at value from a field in another form? here's my form:

<form action="addcomment.php" method="post">
  <input name="name" value="<?php echo $blog['name']; ?>"/>
  <input name="saved_at" value="<?php echo $blog['saved_at']; ?>"/>
  <input type="submit" name="submit" value="Post"/>
</form>

its reads 0.29500000 1348043425 but ISODate in the shell like "saved_at": ISODate("2012-09-19T08: 30: 25.295Z").
How i use its ISODate value to updating document?

Gianfranco

unread,
Sep 20, 2012, 5:29:44 AM9/20/12
to mongod...@googlegroups.com
As I mentioned in line 21, when you output in php you'll need to convert the MongoDate to string with a format similar to:

date('Y-M-d h:i:s', $obj["saved_at"]->sec)

Ardian P. Atmaja

unread,
Sep 26, 2012, 11:14:27 PM9/26/12
to mongod...@googlegroups.com
it's still not working, Gianfranco. it returns like '1970-01-01'. finally i changed the 'saved_at' type to date('Y-m-d H:i:s') on save function so in the collection shows '12-09-20 01-02-16', for example. the new update query is:

mongos> db.blog.update({'name':'myname','saved_at':'12-09-20 01-02-16', {$set: {'comment':'its my first comment for sample.'}};


--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Gianfranco

unread,
Sep 27, 2012, 6:41:43 AM9/27/12
to mongod...@googlegroups.com
Where is this happening in PHP or the Mongo shell (JavaScript) ?

Are you trying to do the update on PHP and verify in the shell? Or the opposite?

It's not clear
Reply all
Reply to author
Forward
0 new messages