如何从mongodb中获取嵌套数据

拉维·谢哈尔
{
    "_id" : "5b606326502f3f055862cfee",
    "Brand" : 5,
    "IsPublished" : false,
    "RowItems" : [ 
        {
            "RowType" : 2,
            "Text" : "AAAA11111",
            "GroupId" : "22e5293c-2dc2-4159-b290-3c8988a27744",
            "LevelIndex" : 1,
            "ItemIndex" : 0,
            "RowItems" : [ 
                {
                    "RowType" : 3,
                    "Text" : "A1",
                    "TagId" : "5762a47cf5f79234582e5dea",
                    "GroupId" : "e660cdce-409c-4f51-b4a5-efc9aafc90bd",
                    "LevelIndex" : 2,
                    "ItemIndex" : 0,
                    "RowItems" : []
                }, 
                {
                    "RowType" : 3,
                    "Text" : "A2",
                    "TagId" : "5762a476f5f79234582e5d7e",
                    "GroupId" : "a0206caa-5793-44ba-aabd-19123e4e50f0",
                    "LevelIndex" : 2,
                    "ItemIndex" : 1,
                    "RowItems" : []
                }, 
                {
                    "RowType" : 3,
                    "Text" : "A3",
                    "TagId" : "578bda4700380f09e02c4df6",
                    "GroupId" : "c1760707-09d9-426f-8331-d4d08a1cd916",
                    "LevelIndex" : 2,
                    "ItemIndex" : 2,
                    "RowItems" : [ 
                        {
                            "RowType" : 2,
                            "Text" : "BBBB",
                            "GroupId" : "7ee73eb0-52d2-4157-88d8-859bfcb76b17",
                            "LevelIndex" : 3,
                            "ItemIndex" : 0,
                            "RowItems" : [ 
                                {
                                    "RowType" : 3,
                                    "Text" : "B1",
                                    "TagId" : "5762a4e0f5f79234582e604f",
                                    "GroupId" : "15f8bf28-8eb4-4d2a-82bf-e4c86e9b78b9",
                                    "LevelIndex" : 4,
                                    "ItemIndex" : 0,
                                    "RowItems" : []
                                }, 
                                {
                                    "RowType" : 3,
                                    "Text" : "B2",
                                    "TagId" : "5762a480f5f79234582e5e12",
                                    "GroupId" : "c299f240-d0dd-402e-a690-3c49e3b6d2b7",
                                    "LevelIndex" : 4,
                                    "ItemIndex" : 1,
                                    "RowItems" : []
                                }
                            ]
                        }, 
                        {
                            "RowType" : 2,
                            "Text" : "CCCC",
                            "GroupId" : "b1f18c28-5cc5-4e41-81b6-6ab688c6a243",
                            "LevelIndex" : 3,
                            "ItemIndex" : 1,
                            "RowItems" : [ 
                                {
                                    "RowType" : 3,
                                    "Text" : "C1",
                                    "TagId" : "578bda4700380f09e02c4df6",
                                    "GroupId" : "dc392245-410d-443a-894f-2fbb6c50ec1d",
                                    "LevelIndex" : 4,
                                    "ItemIndex" : 0,
                                    "RowItems" : []
                                }, 
                                {
                                    "RowType" : 3,
                                    "Text" : "C2",
                                    "TagId" : "5762a480f5f79234582e5e14",
                                    "GroupId" : "1f3ed72b-daf9-48c3-a299-4e7ea7d1f347",
                                    "LevelIndex" : 4,
                                    "ItemIndex" : 1,
                                    "RowItems" : []
                                }
                            ]
                        }
                    ]
                }
            ]
        }, 
        {
            "RowType" : 2,
            "Text" : "BBBB",
            "GroupId" : "1ec01e29-adb7-412f-be92-1a5482ec8311",
            "LevelIndex" : 1,
            "ItemIndex" : 1,
            "RowItems" : [ 
                {
                    "RowType" : 3,
                    "Text" : "Marketing aftersales",
                    "TagId" : "5762a4e0f5f79234582e604f",
                    "GroupId" : "69358bfb-c32e-4910-93b8-6eeabe36daf4",
                    "LevelIndex" : 2,
                    "ItemIndex" : 0,
                    "RowItems" : []
                }, 
                {
                    "RowType" : 3,
                    "Text" : "Modern Retailing",
                    "TagId" : "5762a480f5f79234582e5e28",
                    "GroupId" : "bad86185-0436-40fb-bff1-50602bd0ea5a",
                    "LevelIndex" : 2,
                    "ItemIndex" : 1,
                    "RowItems" : [ 
                        {
                            "RowType" : 2,
                            "Text" : "CCCC",
                            "GroupId" : "da51ea8f-733e-4c19-a546-f90220e28dce",
                            "LevelIndex" : 3,
                            "ItemIndex" : 0,
                            "RowItems" : [ 
                                {
                                    "RowType" : 3,
                                    "Text" : "Modern Retailing",
                                    "TagId" : "5762a480f5f79234582e5e98",
                                    "GroupId" : "760e84d7-6a7c-450a-b669-110fe45c2238",
                                    "LevelIndex" : 4,
                                    "ItemIndex" : 0,
                                    "RowItems" : []
                                }, 
                                {
                                    "RowType" : 3,
                                    "Text" : "Modern Retailing case studies",
                                    "TagId" : "578bda4700380f09e02c4df6",
                                    "GroupId" : "5a77c1c5-facf-4088-b4a6-65f4014e8b81",
                                    "LevelIndex" : 4,
                                    "ItemIndex" : 1,
                                    "RowItems" : []
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ],
    "CreatedDate" : ISODate("2018-07-31T10:24:54.000Z"),
    "CreatedBy" : "AM002GY",
    "UpdatedDate" : ISODate("2018-08-24T10:06:04.189Z"),
    "UpdatedBy" : "AM002GY"
}

我在一个集合中嵌套了 RowItems。我需要根据标签 id 找到任何 rowitem。

如果我通过 TagId ="5762a480f5f79234582e5e28" 那么我需要

{
                        "RowType" : 3,
                        "Text" : "Modern Retailing",
                        "TagId" : "5762a480f5f79234582e5e28",
                        "GroupId" : "bad86185-0436-40fb-bff1-50602bd0ea5a",
                        "LevelIndex" : 2,
                        "ItemIndex" : 1,
                        "RowItems" : [ 
                            {
                                "RowType" : 2,
                                "Text" : "CCCC",
                                "GroupId" : "da51ea8f-733e-4c19-a546-f90220e28dce",
                                "LevelIndex" : 3,
                                "ItemIndex" : 0,
                                "RowItems" : [ 
                                    {
                                        "RowType" : 3,
                                        "Text" : "Modern Retailing",
                                        "TagId" : "5762a480f5f79234582e5e98",
                                        "GroupId" : "760e84d7-6a7c-450a-b669-110fe45c2238",
                                        "LevelIndex" : 4,
                                        "ItemIndex" : 0,
                                        "RowItems" : []
                                    }, 
                                    {
                                        "RowType" : 3,
                                        "Text" : "Modern Retailing case studies",
                                        "TagId" : "578bda4700380f09e02c4df6",
                                        "GroupId" : "5a77c1c5-facf-4088-b4a6-65f4014e8b81",
                                        "LevelIndex" : 4,
                                        "ItemIndex" : 1,
                                        "RowItems" : []
                                    }
                                ]
                            }
米克尔

欢迎使用 Stack Overflow @ravi-shekhar。作为对未来的提示,如果你展示你尝试过的东西,这是一种很好的形式,人们会更倾向于以这种方式指导你。

这个答案可能有帮助:如何使用 elemMatch 来匹配嵌套数组

您可以查看使用 $elemMatch

困难可能是您的嵌套结构......

如果你在你的第一级搜索,RowItems你会做这样的查询:

{
  "RowItems": {
    $elemMatch: {
      GroupId: "a0206caa-5793-44ba-aabd-19123e4e50f0"
    }
  }
}

我认为因为它是嵌套的,你可以这样做:

{
  "RowItems": {
    $elemMatch: {
      "RowItems": {
        $elemMatch: {
          TagId: "5762a47cf5f79234582e5dea"
        }
      }
    }
  }
}

我没有试过这段代码,所以它可能不起作用,但应该有助于你思考这个问题

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何从嵌套数组中获取数据

来自分类Dev

如何在MongoDB中更新嵌套数组中的数据

来自分类Dev

如何从Laravel上的快速加载中获取嵌套数据?

来自分类Dev

如何从嵌套数组中获取数据[Laravel]

来自分类Dev

如何通过php从嵌套数组中获取API数据

来自分类Dev

如何从JSON AngularJS获取嵌套数据

来自分类Dev

从嵌套数组中获取数据

来自分类Dev

无法获取嵌套数据中的密钥

来自分类Dev

如何在MongoDB中的嵌套数组中获取子文档?

来自分类Dev

如何在Mongodb中的嵌套数组中添加/推送数据

来自分类Dev

将MongoDB中的嵌套数据放入Pandas数据框

来自分类Dev

MongoDB插入嵌套数据

来自分类Dev

MongoDB在双嵌套数组中获取最大日期

来自分类Dev

MongoDB 只从嵌套数组中获取匹配的元素

来自分类Dev

如何在mongoDB的嵌套数据中使用聚合$ lookup?

来自分类Dev

将数据插入MongoDB中的嵌套数组

来自分类Dev

直接从AngulaJS中的嵌套数组获取数据

来自分类Dev

使用php从json中的嵌套数组获取数据

来自分类Dev

直接从AngulaJS中的嵌套数组获取数据

来自分类Dev

Ionic 2 从嵌套数组中获取数据

来自分类Dev

获取列表中嵌套数据框的摘要统计信息

来自分类Dev

MongoDB:如何从嵌套数组中的文档中删除对象

来自分类Dev

MongoDB - 如何从嵌套数组中的元素中删除字段?

来自分类Dev

如何在Firebase数据库中嵌套数据?

来自分类Dev

如何在mongodb中查询对象的嵌套数组?

来自分类Dev

如何在mongoDB或Mongoose中更新嵌套数组的值

来自分类Dev

如何在MongoDB中的$ group内创建嵌套数组?

来自分类Dev

MongoDB:如何在嵌套数组中设置字段值

来自分类Dev

如何限制在mongodb中返回的嵌套数组字段

Related 相关文章

热门标签

归档