php - MongoDB 从集合中提取数组元素

标签 php arrays mongodb pull

我有一个 mongodb 对象如下:

array (
  '_id' => new MongoId("4cc97fb0247ae8747ec5fefb"),
  'posts' => 
  array (
    0 => 
    array (
      'comment' => 'Eamorr',
      'fromUname' => 'Eamorr',
      'time' => 1288273840,
      'UTC' => '2010-10-28T14:50:40+01:00',
      'ip' => '127.0.0.1',
      'id' => '123lasdfiqwoei28asdf',
    ),
    1 => 
    array (
      'comment' => 'Hello',
      'fromUname' => 'Eamorr',
      'time' => 1288277023,
      'UTC' => '2010-10-28T15:43:43+01:00',
      'ip' => '127.0.0.1',
      'id' => 'qopqwier982389qwfa',
    ),
    2 => 
    array (
      'comment' => 'Hello',
      'fromUname' => 'Anonymous',
      'time' => 1288283506,
      'UTC' => '2010-10-28T17:31:46+01:00',
      'ip' => '127.0.0.1',
      'id' => 'ioqwoeias892398wrf',
    ),
    /////
    //Want to remove element 3:
    /////
    3 => 
    array (
      'comment' => 'asdfasadf',
      'fromUname' => 'Anonymous',
      'time' => 1288283864,
      'UTC' => '2010-10-28T17:37:44+01:00',
      'ip' => '127.0.0.1',
      'id' => 'wwwwwiasdfn234oiasf',
    ),
    4 => 
    array (
      'comment' => 'asdfasdfasdf',
      'fromUname' => 'Anonymous',
      'time' => 1288284076,
      'UTC' => '2010-10-28T17:41:16+01:00',
      'ip' => '127.0.0.1',
      'id' => '290qwefoiqweproiqwerpq',
    ),
    5 => 
    array (
      'comment' => 'ASDF',
      'fromUname' => 'Eamorr',
      'time' => 1288284331,
      'UTC' => '2010-10-28T17:45:31+01:00',
      'ip' => '127.0.0.1',
      'id' => 'eioqw8923892hasdf',
    ),
    6 => 
    array (
      'comment' => 'ASDF2',
      'fromUname' => 'Eamorr',
      'time' => 1288284370,
      'UTC' => '2010-10-28T17:46:10+01:00',
      'ip' => '127.0.0.1',
      'id' => '23oaiofsaij234',
    ),
  ),
  'uname' => 'Eamorr',
)

现在,我正在编写一些 PHP 代码来删除帖子[x]。我正在尝试使用 $pull 删除数组元素。

我有一个“id”“wwwwwiasdfn234oiasf”(数组元素 3),我想删除这个整个数组元素,只在“posts”数组中留下 6 个元素。

我试过谷歌搜索和查找文档都无济于事......我仍然无法掌握 mongodb 语法。

我正在用 PHP 完成所有这些工作,但任何语言都可以,我应该能够进行翻译。

非常感谢,

解决方案(在 PHP 中):

$uname=whatever
$id=whatever
$mongo=new Mongo();
$walls=$mongo->people->walls;
$walls->update(array('uname'=>$uname),array('$pull'=>array('posts'=>array('id'=>$id))));

最佳答案

以下是使用 MongoDB shell 执行此操作的方法。您应该能够将其翻译成 PHP。

拉取操作由$pull修饰符、字段选择器值表达式组成。

{ $pull: { fieldSelector: valueExpression } }

在您的例子中,字段选择器是 posts,因为这是您要更新的数组。用简单的英语来说,值表达式是

where the id of the post equals "wwwwwiasdfn234oiasf"

这转换为 { id: "wwwwwiasdfn234oiasf"}。如果我们将所有这些结合起来,您将得到以下 $pull 语句,它将从数组中删除所需的项目:

{ $pull: { posts: { id: "wwwwwiasdfn234oiasf" } } }

关于php - MongoDB 从集合中提取数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4058748/

相关文章:

php - 通过带分隔符的字符串访问多维数组

php - Memcache 中的 replace 函数有什么意义?

php - 将 Monolog 添加到 PHP 5.6 项目

javascript - 如何在 javascript 中将数组从 php 返回到 ajax 响应

c - 直接在二维数组中执行 fisher - yates shuffle

arrays - Delphi将动态记录数组传递给函数

java - 在 Morphia 中,我如何更新 ArrayList 中的一个嵌入对象

java - 如何关闭 fakemongo 日志

php - 使用 XMLReader、DOM、Xpath 获取给定节点的标签名称和值

java - 如何使用 Collectors.summarizingInt 和 flatMap 运算符的用法来总结歌曲的投票