3

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
  • Be careful about what type of $id you pass on to MongoDB. The following could save you a few times: $id = new MongoId($id); Commented Jan 17, 2011 at 13:48

1 Answer 1

15

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" } } }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.