mongoDB更新无法正常工作

发布于 2024-11-30 01:46:29 字数 984 浏览 7 评论 0原文

对于“插入”,我完全同意。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢你一世 2024-12-07 01:46:29

你的代码应该没问题。

当您开发代码时,您可能对其进行了几次测试,并使用错误的修饰符文档更新了文档。现在里面全是垃圾。

只需使用干净的文档进行测试即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文