mongoDB更新无法正常工作
对于“插入”,我完全同意。
PHP - MongoDB 以我想要的方式插入,但更新无法按我的计划进行。
紧接着“插入”没有“ip”和“时间”字段的文档。
'ip' 字段工作完全正常 - {'ip':'127.0.0.1'}
但不知何故 'time' 数组字段的行为很奇怪。
{'time':{'realtime':'2011-01-01 12:00:00', 'mongotime': ISODate("2011-01-01T00:00:00")}}
"时间" 应插入该文件应该没问题。
但是这个“time”数组和独立的“realtime”、“mongotime”都被插入到文档中。
结果,我得到
{'time':{'realtime':'2011-01-01 12:00:00', 'mongotime': ISODate("2011-01-01T00:00:00")} , '实时':'2011-01-01 12:00:00', 'mongotime': ISODate("2011-01-01T00:00:00")}
我不知道为什么会发生这种情况......
这太奇怪了。
这是我的代码:
$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));
我是否以错误的方式理解 mongoDB 或者它的行为不正确?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的代码应该没问题。
当您开发代码时,您可能对其进行了几次测试,并使用错误的修饰符文档更新了文档。现在里面全是垃圾。
只需使用干净的文档进行测试即可。
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.