MongoDB 从集合中提取数组元素 [英] MongoDB pull array element from a collection
问题描述
我有一个 mongodb 对象如下:
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',
)
现在,我正在编写一些 PHP 代码来删除帖子 [x].我正在尝试使用 $pull 删除数组元素.
Now, I am writing some PHP code to remove posts[x]. I'm trying to use $pull to remove the array element.
我有一个 'id' 'wwwwwiasdfn234oiasf'(数组元素 3),我想删除这个整个数组元素,只在 'posts' 数组中留下 6 个元素.
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.
我试过谷歌搜索并查找文档无济于事......我仍然无法掌握 mongodb 语法.
I've tried googling and looking up the documentation to no avail... I still can't get the hang of the mongodb syntax.
我正在用 PHP 完成所有这些工作,但任何语言都可以,我应该能够进行翻译.
I'm doing all this in PHP, but any language will do I should be able to do the translation.
非常感谢,
解决方案(在 PHP 中):
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))));
推荐答案
以下是使用 MongoDB shell 的方法.您应该可以将其翻译成 PHP.
Here's how to do it using the MongoDB shell. You should be able to translate it into PHP.
拉取操作由$pull
修饰符、字段选择器和值表达式组成.
A pull operation consists of the $pull
modifier, a field selector and a value expression.
{ $pull: { fieldSelector: valueExpression } }
在您的情况下,字段选择器是 posts
,因为这是您要更新的数组.简单来说,值表达式是
In your case the field selector is posts
, since that's the array you want to update. The value expression, in plain English, is
其中帖子的 id
等于wwwwwiasdfn234oiasf"
where the
id
of the post equals "wwwwwiasdfn234oiasf"
这转化为 { id: "wwwwwiasdfn234oiasf" }
.如果我们将所有这些结合起来,您将得到以下 $pull
语句,它将从数组中删除所需的项目:
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屋!