With ‘Insert’ I’m totally okay with it.
PHP – MongoDB inserts way I wanted to but update does not work as I planned.
Right after ‘Insert’ the document which does not have “ip” and “time” fields.
‘ip’ field works totally fine – {‘ip’:’127.0.0.1′}
but somehow ‘time’ array field behaves strangely.
{‘time’:{‘realtime’:’2011-01-01 12:00:00′, ‘mongotime’: ISODate(“2011-01-01T00:00:00”)}}
“time” should be inserted to the document and should be okay.
But both of this “time” array and independent ‘realtime’, ‘mongotime’ is inserted to the document.
As a result, I’m getting
{‘time’:{‘realtime’:’2011-01-01 12:00:00′, ‘mongotime’: ISODate(“2011-01-01T00:00:00”)}, ‘realtime’:’2011-01-01 12:00:00′, ‘mongotime’: ISODate(“2011-01-01T00:00:00”)}
I don’t know why this happens…
This is so weird.
Here is my code:
$query = array('username'=>"$username");
$realtime = date("Y-m-d H:i:s");
$mongotime = new Mongodate(strtotime($realtime));
$set_item = array('ip'=>"$IP", 'time'=> array('realtime'=>"$realtime",
'mongotime'=> $mongotime));
$db->member->update($query, array('$set' => $set_item));
Do I understand mongoDB in wrong way or does it behave improperly?
Your code should be OK.
As you developed your code you probably tested it a couple of times and updated the document with bad modifier documents. Now there’s garbage in it.
Just test it with a clean document.