如何从mongodb获取嵌套数据

时间:2018-08-24 13:47:38

标签: 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" : []
                                    }
                                ]
                            }

1 个答案:

答案 0 :(得分:0)

欢迎来到堆栈溢出@ ravi-shekhar。就像对未来的提示一样,这是一个很好的形式,如果您展示自己尝试过的内容,人们将更倾向于以这种方式指导您。

此答案可能会有所帮助:How to use elemMatch to match nested array

您可以查看$elemMatch

的用法

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

如果您在第一级RowItems中进行搜索,则会执行以下查询:

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

我认为因为它是嵌套的,所以您可以执行以下操作:

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

我没有尝试过这段代码,因此它可能无法正常工作,但应该可以帮助您思考此问题