MongoDB/PHP:从数组中删除元素
您好,
我有以下 MongoDB 对象:
{
"_id": ObjectId("4d0e28938b012fe28754715a"),
"notifications": {
"0": {
"type": "privateMessage",
"fromUname": "Eamorr2",
"time": 1292773522,
"id": "1lfw70h789u13a1e67pv"
},
"1": {
"type": "privateMessage",
"fromUname": "Eamorr2",
"time": 1292773522,
"id": "iwoidjsoskqp23nlwof"
}
},
"toUname": "Eamorr"
}
我正在尝试删除元素 0,留下我:
{
"_id": ObjectId("4d0e28938b012fe28754715a"),
"notifications": {
"0": {
"type": "privateMessage",
"fromUname": "Eamorr2",
"time": 1292773522,
"id": "iwoidjsoskqp23nlwof"
}
},
"toUname": "Eamorr"
}
这是我迄今为止尝试过的(在 PHP 中),但无济于事:
$customerNotifications->update(array('toUname'=>$uname),array('$pull'=>array('notifications'=>$key)));
但它不起作用,我现在完全陷入困境。
非常感谢任何帮助。提前致谢,
Greetings,
I have the following MongoDB object:
{
"_id": ObjectId("4d0e28938b012fe28754715a"),
"notifications": {
"0": {
"type": "privateMessage",
"fromUname": "Eamorr2",
"time": 1292773522,
"id": "1lfw70h789u13a1e67pv"
},
"1": {
"type": "privateMessage",
"fromUname": "Eamorr2",
"time": 1292773522,
"id": "iwoidjsoskqp23nlwof"
}
},
"toUname": "Eamorr"
}
I'm trying to delete element 0, to leave me with:
{
"_id": ObjectId("4d0e28938b012fe28754715a"),
"notifications": {
"0": {
"type": "privateMessage",
"fromUname": "Eamorr2",
"time": 1292773522,
"id": "iwoidjsoskqp23nlwof"
}
},
"toUname": "Eamorr"
}
Here's what I've tried sofar (in PHP), to no avail:
$customerNotifications->update(array('toUname'=>$uname),array('$pull'=>array('notifications'=>$key)));
But it's not working and I'm totally stuck now.
Any help much appreciated. Thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Eamorr,
$pull 运算符不适用于您正在使用的文档,因为“notifications”键并不是真正的数组。它更像是一个嵌入式文档,带有编号键,使其表面上类似于一个数组。
(据我所知)没有办法保留此文档结构并自动重命名编号键。
如果您稍微重构您的文档,看起来像这样:
元素仍然会隐式编号。现在它是一个数组,因此您可以免费获得它。
您可以像这样使用 $pull 运算符(我不熟悉 PHP 驱动程序,所以我给您等效的 shell):
我任意使用“toUname”键来识别文档,但我猜您会想要使用 _id-field。另外,我使用消息的“id”键来标识要从数组中提取的消息,因为它更安全,并且确保您不会意外删除错误的消息,以防数组发生更改您确定了要删除的数组序号。
我希望这有帮助。
Eamorr,
The $pull operator will not work on the document you are using, because the "notifications"-key is not really an array. It is rather an embedded document, with numbered keys, making it superficially resemble an array.
There is no way (that I know of) to keep this document structure and have the numbered keys renamed automatically.
If you refactor your document slightly, to look like this:
The elements will still be numbered, implicitly. It's now an array, so you get that for free.
You can use the $pull operator like this (I am not familiar with the PHP-driver, so I'm giving you the shell equivalent):
I arbitrarily used the "toUname" key to identify the document, but I guess you will be wanting to use the _id-field. Also, I'm using the "id"-key of the messages to identify the message to pull from the array, as it is a lot safer and makes sure you don't accidentally remove the wrong message in case the array has changed since you identified the array ordinal to remove.
I hope that helps.