我有一个 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' 'wwwiasdfn234oiasf' (数组元素 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))));