MongoDB pull array element from a collection

三世轮回 提交于 2019-11-28 09:31:39

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" } } }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!