首页 > 解决方案 > 在 mongodb 聚合中的数组内查找嵌套对象字段

问题描述

我有这个对象如下。

{
    "_id" : ObjectId("5ec80a981e89a84b19934039"),
    "status" : "active",
    "organizationId" : "1",
    "productId" : "1947",
    "name" : "BOOKEND & PAPER WEIGHT SET – ZODIAC PIG – RED COPPER + PLATINUM",
    "description" : "This global exclusive Zodiac bookend and paperweight set from Zuny will stand auspiciously on your bookcase and table, spreading good luck and fortune throughout your home just in time for the Year of the Pig.",
    "brand" : "ZUNY",
    "created" : "2018-09-28 00:00:00",
    "updated" : "2020-05-22 09:19:07",
    "mainImage" : "https://",
    "availableOnline" : true,
    "colors" : [ 
        {
            "images" : [ 
                {
                    "type" : "studio",
                    "url" : "https://"
                }, 
                {
                    "type" : "studio",
                    "url" : "https://"
                }, 
                {
                    "type" : "studio",
                    "url" : "https://"
                }
            ],
            "extraInfo" : [ 
                {
                    "type" : "text-tag",
                    "title" : "CATEGORY",
                    "tags" : [ 
                        "HOME FURNISHING & DÉCOR", 
                        "LIFESTYLE"
                    ]
                }, 
                {
                    "type" : "text-tag",
                    "title" : "BRAND",
                    "tags" : [ 
                        "ZUNY"
                    ]
                }, 
                {
                    "type" : "text-tag",
                    "title" : "COLOUR",
                    "tags" : [ 
                        "GOLD", 
                        "ROSE GOLD"
                    ]
                }, 
                {
                    "type" : "text-tag",
                    "title" : "SEASON",
                    "tags" : [ 
                        "AW(2018)"
                    ]
                }, 
                {
                    "type" : "text-tag",
                    "title" : "HASHTAG",
                    "tags" : [ 
                        "BOOKCASES", 
                        "BOOKEND", 
                        "COLOUR", 
                        "EXCLUSIVE", 
                        "GLOBAL EXCLUSIVE", 
                        "HOME", 
                        "LEATHER", 
                        "MOTIF", 
                        "OBJECTS", 
                        "PAPER", 
                        "PAPERWEIGHT", 
                        "PLATINUM", 
                        "SET", 
                        "SYNTHETIC", 
                        "ZODIAC", 
                        "HANDMADE", 
                        "time"
                    ]
                }
            ],
            "_id" : ObjectId("5ec80a981e89a84b1993403a"),
            "colorId" : "1",
            "color" : "ROSE GOLD",
            "status" : "active",
            "sizes" : [ 
                {
                    "extraInfo" : [ 
                        {
                            "type" : "text-block",
                            "title" : "Size And Fit",
                            "text" : ""
                        }, 
                        {
                            "type" : "text-block",
                            "title" : "Information",
                            "text" : "Global exclusive. Colour: Copper/Platinum. Set includes: Zodiac Pig bookend (x 1), Zodiac Pig paperweight (x 1). Metallic copper- and platinum-tone synthetic leather. Pig motif. Iron pellet filling. Handmade"
                        }
                    ],
                    "_id" : ObjectId("5ec80a981e89a84b1993403b"),
                    "sizeId" : "1",
                    "neo" : "0210111790664",
                    "size" : "*",
                    "originalPrice" : "1060.00",
                    "sellingPrice" : "1060.00",
                    "discountPercent" : "0.00",
                    "url" : "https://",
                    "status" : "active",
                    "currency" : "HK$",
                    "stores" : [ 
                        {
                            "storeId" : "1",
                            "quantity" : 70,
                            "_id" : ObjectId("5ec80a981e89a84b1993403c"),
                            "available" : 70,
                            "reserved" : 0,
                            "name" : "Park Street",
                            "status" : "active"
                        }, 
                        {
                            "storeId" : "2",
                            "quantity" : 95,
                            "_id" : ObjectId("5ec80a981e89a84b1993403d"),
                            "name" : "Rashbehari",
                            "status" : "active"
                        }
                    ]
                }
            ]
        }
    ],
    "__v" : 0
}

我想要输出如下

{
        "name": "Mock Collection",
        "collectionId": "92",
        "products": [
            {
                "title": "GLOBAL EXCLUSIVE OFF-SHOULDER SHIRT DRESS",
                "imageUrl": "https://",
                "productId": "21174",
                "currency": "" // This should be this.colors[0].sizes[0].currency
            },
        ]
    }

如何获取嵌套字段。我尝试使用 arrayElemAt,通过它我能够获得颜色 [0]。但是我很困惑如何从那里进入大小的嵌套对象。货币节点也应该具有准确的值。它就像我不想要的货币:{货币:价值}。

请帮忙!

标签: mongodbmongodb-queryaggregation-framework

解决方案


不确定您是如何获得该输出的,但是currency要从第一个大小对象中提取,那么您需要尝试以下操作:

db.collection.aggregate([
  {
    $project: {
      currency: {
        $arrayElemAt: [
          {
            $arrayElemAt: [ "$colors.sizes.currency", 0 ] // gives an array of currency values, in your case since you've only one object just an array of one value
          },
          0
        ]
      }
    }
  }
])

测试: mongoplayground


推荐阅读