MongoDB pull array element from a collection

2019-01-18 08:30发布

问题:

I have a mongodb object as follows:

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',
)

Now, I am writing some PHP code to remove posts[x]. I'm trying to use $pull to remove the array element.

I have an 'id' 'wwwwwiasdfn234oiasf' (array element 3) and I want to remove this entire array element, leaving just 6 elements in the 'posts' array.

I've tried googling and looking up the documentation to no avail... I still can't get the hang of the mongodb syntax.

I'm doing all this in PHP, but any language will do I should be able to do the translation.

Many thanks in advance,

Solution (in PHP):

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

回答1:

Here's how to do it using the MongoDB shell. You should be able to translate it into PHP.

A pull operation consists of the $pull modifier, a field selector and a value expression.

{ $pull: { fieldSelector: valueExpression } }

In your case the field selector is posts, since that's the array you want to update. The value expression, in plain English, is

where the id of the post equals "wwwwwiasdfn234oiasf"

This translates to { id: "wwwwwiasdfn234oiasf" }. If we combine all of this, you'll get the following $pull statement, which will remove the desired item from the array:

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