簡體   English   中英

如何從mongodb獲取嵌套數據

[英]How to fetch nested data from 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"
        }
      }
    }
  }
}

我沒有嘗試過此代碼,因此它可能無法正常工作,但應該可以幫助您思考此問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM