删除嵌套文档数组中的嵌入文档 [英] Remove embedded document in a nested array of documents

查看:22
本文介绍了删除嵌套文档数组中的嵌入文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的架构如下所示:

"content" : [
        {
            "_id" : ObjectId("4fc63de85b20fb72290000f8"),
            "assets" : [
                {
                    "path" : "temp/4f840af9565832fa14000002/4f840b1e565832fa14000007/4fc63de85b20fb72290000f7/content/I_Understanding_and_Measuring.pdf",
                    "_id" : ObjectId("4fc63def5b20fb722900010e")
                },
                {
                    "path" : "temp/4f840af9565832fa14000002/4f840b1e565832fa14000007/4fc63de85b20fb72290000f7/content/me.jpg",
                    "_id" : ObjectId("4fc63e4d5b20fb722900015d")
                }
            ],
            "content" : "",
            "name" : "Downloads"
        },
        {
            "_id" : ObjectId("4fc63dfd5b20fb722900012a"),
            "assets" : [
                {
                    "path" : "temp/4f840af9565832fa14000002/4f840b1e565832fa14000007/4fc63de85b20fb72290000f7/content/me.jpg",
                    "_id" : ObjectId("4fc63e055b20fb7229000147")
                },
                {
                    "path" : "temp/4f840af9565832fa14000002/4f840b1e565832fa14000007/4fc63de85b20fb72290000f7/content/thierry-henry-12-31-11-1.jpg",
                    "_id" : ObjectId("4fc63e525b20fb7229000164")
                }
            ],
            "content" : "",
            "name" : "Bio"
        }
    ],

我可以通过以下方式检索此文档:

I can retrieve this document with:

db.presentations.find({'content.assets._id': ObjectId('4fc63def5b20fb722900010e')})`

我尝试了以下操作从资产集合中删除文档,但没有成功:

I've tried the following to remove an document from the assets collection with the line below, but to no avail:

db.presentations.update(
  {'content.assets._id': ObjectId('4fc63def5b20fb722900010e')}, 
  {$pull: {'content.assets': {'_id': ObjectId('4fc63def5b20fb722900010e')}}}
)

我正在尝试通过其 id 从相应的 assets 集合中删除一个项目.有什么想法吗?

I'm trying to remove an item from the corresponding assets collection by its id. Any ideas?

推荐答案

你们离得这么近!请记住,您最外层的内容"本身就是一个数组.因此,以下 2 个字符更改有效,请在 $pull 的值中使用 content.$.assets.

You are so close! Remember that your outermost "content" is an array itself. So the following 2 character change works, use content.$.assets inside the value for $pull.

db.presentations.update(
  {'content.assets._id': ObjectId('4fc63def5b20fb722900010e')}, 
  {$pull: {'content.$.assets': {'_id': ObjectId('4fc63def5b20fb722900010e')}}}
)

放大.

这篇关于删除嵌套文档数组中的嵌入文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆