在MongoDB聚合中的数组内查找嵌套对象字段

时间:2020-06-18 15:50:15

标签: mongodb mongodb-query aggregation-framework

我有这个对象,如下。

{
    "_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,通过它我可以进入colors [0]。但是我很困惑如何从那里进入大小嵌套的对象。货币节点也应具有确切的值。就像我不需要的货币:{currency:value}。

请帮助!

1 个答案:

答案 0 :(得分:1)

不确定如何获得输出,但是要从第一个大小的对象中提取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