Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

php - MongoDB pull array element from a collection

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))));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...