Mongodb在嵌入文档中查找操作

问题描述 投票:0回答:1

我的需求是通过Java在mongodb集合中创建一个嵌入文档,结构如下

文件夹

{

_id: String, // location id

documentVersion: int,

locationDisplayName: String;

displayName: String,

subfolders: [

    {

        dispalyName: String,

        subcategories: [

            /// For Catgeory type

            {

                displayName: String,

                subcategories: [

                ],

                items: [

                ]

            },

        ],

        items: [

            {

                displayName: String,

                itemId: String,

                name: String,

                code: String

            },

            ...



        ],

        itemId: String,

        name: String,

        code: String

    }

],

items: [

    {

        displayName: String,

        itemId: String,

        name: String,

        code: String

    },

    ..

]

}

我可以使用元素匹配检查父文件夹并在 2 层插入子文件夹。但是我的子文件夹可以达到第 6 级。我怎样才能在正确的级别进行匹配和插入文件夹,并将项目插入到文件夹中

java mongodb collections document
1个回答
0
投票

如您所知,MongoDB 中的嵌套结构是使用位置运算符进行更新和引用的。

它已记录在所附页面中。您还可能会看到下面的示例,引用自同一文档。该文档最初包含在另一个 Stack-over-flow 答案中,该答案也附在其中。

由于这些文档基于旧版本的 MongoDB,因此请求您也可以参考最新的文档。你可以看到它太封闭了。

//Update all matching documents in nested array**
db.coll.update({}, {$set: {“a.$[i].c.$[j].d”: 2}}, {arrayFilters: [{“i.b”: 0}, {“j.d”: 0}]})
Input: {a: [{b: 0, c: [{d: 0}, {d: 1}]}, {b: 1, c: [{d: 0}, {d: 1}]}]}
Output: {a: [{b: 0, c: [{d: 2}, {d: 1}]}, {b: 1, c: [{d: 0}, {d: 1}]}]}

位置运算符匹配嵌套数组

Mongodb 更新深层嵌套子文档

与 $[] 结合更新嵌套数组

© www.soinside.com 2019 - 2024. All rights reserved.