MongoDB/PHP:从数组中删除元素 [英] MongoDB/PHP: delete element from array

查看:70
本文介绍了MongoDB/PHP:从数组中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我有以下 MongoDB 对象:

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"
}

我正在尝试删除元素 0,以便留下:

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"
}

这是我迄今为止尝试过的(在 PHP 中),但无济于事:

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,

推荐答案

Eamorr,

$pull 操作符不适用于您正在使用的文档,因为通知"键并不是真正的数组.它是一个嵌入式文档,带有编号的键,使其表面上类似于一个数组.没有办法(据我所知)保留此文档结构并自动重命名编号键.

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:

{
   "notifications": [
    {
       "type": "privateMessage",
       "fromUname": "Eamorr2",
       "time": 1292773522,
       "id": "1lfw70h789u13a1e67pv"
    },
    {
       "type": "privateMessage",
       "fromUname": "Eamorr2",
       "time": 1292773522,
       "id": "iwoidjsoskqp23nlwof"
    }
  ],
   "toUname": "Eamorr"
}

元素仍将被隐式编号.它现在是一个数组,所以你可以免费获得它.您可以像这样使用 $pull 运算符(我不熟悉 PHP 驱动程序,所以我给您提供了等效的 shell):

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):

db.messages.update({ "toUname" : "Eamorr" }, { $pull : { "notifications" : { "id" : "1lfw70h789u13a1e67pv" }}});

我随意使用toUname"键来标识文档,但我猜您会想要使用 _id 字段.此外,我使用消息的id"键来识别要从数组中提取的消息,因为它更安全,并确保您不会意外删除错误消息,以防数组发生更改您确定了要删除的数组序号.

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.

希望能帮到你.

这篇关于MongoDB/PHP:从数组中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆