MongoDB中从集合拉数组元素 [英] MongoDB pull array element from a collection
问题描述
我有一个MongoDB的对象如下:
阵列(
'_id'=>新MongoId(4cc97fb0247ae8747ec5fefb),
'帖子'=>
阵列(
0 =>
阵列(
评论= GT; Eamorr',
fromUname'=> Eamorr',
'时间'=> 1288273840,
'UTC'=> 2010-10-28T14:50:40 + 01:00,
IP=> 127.0.0.1,
'ID'=> 123lasdfiqwoei28asdf',
)
1 =>
阵列(
评论= GT; '你好',
fromUname'=> Eamorr',
'时间'=> 1288277023,
'UTC'=> 2010-10-28T15:43:43 + 01:00,
IP=> 127.0.0.1,
'ID'=> qopqwier982389qwfa',
)
2 =>
阵列(
评论= GT; '你好',
fromUname'=> '匿名',
'时间'=> 1288283506,
'UTC'=> 2010-10-28T17:31:46 + 01:00,
IP=> 127.0.0.1,
'ID'=> ioqwoeias892398wrf',
)
/////
//要删除元素3:
/////
3 =>
阵列(
评论= GT; asdfasadf',
fromUname'=> '匿名',
'时间'=> 1288283864,
'UTC'=> 2010-10-28T17:37:44 + 01:00,
IP=> 127.0.0.1,
'ID'=> wwwwwiasdfn234oiasf',
)
4 =>
阵列(
评论= GT; asdfasdfasdf',
fromUname'=> '匿名',
'时间'=> 1288284076,
'UTC'=> 2010-10-28T17:41:16 + 01:00,
IP=> 127.0.0.1,
'ID'=> 290qwefoiqweproiqwerpq',
)
5 =>
阵列(
评论= GT; ASDF,
fromUname'=> Eamorr',
'时间'=> 1288284331,
'UTC'=> 2010-10-28T17:45:31 + 01:00,
IP=> 127.0.0.1,
'ID'=> eioqw8923892hasdf',
)
6 =>
阵列(
评论= GT; ASDF2',
fromUname'=> Eamorr',
'时间'=> 1288284370,
'UTC'=> 2010-10-28T17:46:10 + 01:00,
IP=> 127.0.0.1,
'ID'=> 23oaiofsaij234',
)
)
'的uname => Eamorr',
)
现在,我写一些PHP code删除帖子[X]。我试图用$拉删除数组元素。
我有一个'身份证'wwwwwiasdfn234oiasf(数组3),我想删除此的全部的数组元素,只留下6要素的帖子阵列。
我试过谷歌搜索和查找的文档无济于事......我仍然不能得到MongoDB的语法的窍门。
我做的这一切在PHP,但任何语言都行,我应该能够做翻译。
在事先非常感谢,
解决方案(在PHP):
$ =的uname什么
$ ID =什么
$蒙戈=新蒙戈();
$墙壁= $ mongo->以人为>墙壁;
$walls->update(array('uname'=>$uname),array('$pull'=>array('posts'=>array('id'=>$id))));
下面是如何使用MongoDB的shell来做到这一点。你应该能够把它翻译成PHP。
一个拉操作由 $拉
调节剂,字段选择器和价值EX pression 强>。
{$拉:{fieldSelector:valueEx pression}}
在你的情况字段选择器是帖子
,因为这是要更新的阵列。该值前pression,用简单的英语,是
在这里的帖子的
ID等于wwwwwiasdfn234oiasf
块引用>这转化为
{ID:wwwwwiasdfn234oiasf}
。如果我们结合了这一切,你会得到以下$拉
语句,将从数组中删除所需的项目:{$拉:{帖子:{ID:wwwwwiasdfn234oiasf}}}
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))));
解决方案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, iswhere 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" } } }
这篇关于MongoDB中从集合拉数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!