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

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

问题描述

我的模式如下:

"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?

推荐答案

您是如此亲密!请记住,您最外层的内容"本身就是一个数组.因此,以下两个字符更改有效,请在$ 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天全站免登陆