MongoDB聚合匹配嵌套数组

时间:2020-06-08 12:26:26

标签: javascript node.js arrays mongodb

我正在努力从Mongo DB获取一些汇总数据。我有以下收藏: 我正在努力从Mongo DB获得一些汇总数据。我有以下收藏: 我有2个收藏集:

产品

 {
        "_id" : ObjectId("5eda578cb9c3fa1ef8c483c5"),
        "brand_name" : "LAVA",
        "short_description" : "Specifications",
        "offer_price" : 20,
        "quantity" : 5,
        "variants" : [ 
            "5eb9300438d0b83a3088feec", 
            "5eb92a7909823240081cd763"
        ],
        "seller_id" : ObjectId("5e994af17a2f9b3a007e247c"),
        "name" : "LAVA X10",
        "createdAt" : ISODate("2020-06-05T14:32:44.582Z"),
        "updatedAt" : ISODate("2020-06-05T14:32:44.582Z")
    }, 
    {
        "_id" : ObjectId("5eda578cb9c3fa1ef8c483c8"),
        "brand_name" : "LAVA",
        "short_description" : "Specifications",
        "offer_price" : 20,
        "quantity" : 5,
        "variants" : [ 
            "5eb9300438d0b83a3088feed", 
            "5eb92a7909823240081cd763"
        ],
        "seller_id" : ObjectId("5e994af17a2f9b3a007e247c"),
        "name" : "LAVA X11",
        "createdAt" : ISODate("2020-06-05T14:32:44.582Z"),
        "updatedAt" : ISODate("2020-06-05T14:32:44.582Z")
    }

2。属性

{
    "_id" : ObjectId("5eb92a4d09823240081cd75f"),
    "is_active" : true,
    "name" : "Color",
    "slug" : [  
        {
            "is_active" : true,
            "_id" : ObjectId("5eb92a7909823240081cd763"),
            "name" : "Green",
            "updatedAt" : ISODate("2020-06-01T13:47:56.584Z"),
            "createdAt" : ISODate("2020-05-11T10:35:37.357Z")
        }, 
        {
            "is_active" : true,
            "_id" : ObjectId("5eb92a7909823240081cd764"),
            "name" : "RED",
            "updatedAt" : ISODate("2020-06-01T13:47:39.236Z"),
            "createdAt" : ISODate("2020-05-11T10:35:37.357Z")
        }
    ],
    "createdAt" : ISODate("2020-05-11T10:34:53.642Z"),
    "updatedAt" : ISODate("2020-06-01T13:48:09.194Z"),
    "__v" : 0
},
{
    "_id" : ObjectId("5eb92a5409823240081cd760"),
    "is_active" : true,
    "name" : "Size",
    "slug" : [ 
        {
            "is_active" : true,
            "_id" : ObjectId("5eb92c4338d0b83a3088feeb"),
            "name" : "M",
            "updatedAt" : ISODate("2020-06-01T13:47:08.993Z"),
            "createdAt" : ISODate("2020-05-11T10:43:15.355Z")
        },
        {
            "is_active" : true,
            "_id" : ObjectId("5eb9300438d0b83a3088feec"),
            "name" : "S",
            "updatedAt" : ISODate("2020-05-11T10:59:16.731Z"),
            "createdAt" : ISODate("2020-05-11T10:59:16.731Z")
        }, 
        {
            "is_active" : true,
            "_id" : ObjectId("5eb9300438d0b83a3088feed"),
            "name" : "XXL",
            "updatedAt" : ISODate("2020-06-01T13:46:36.417Z"),
            "createdAt" : ISODate("2020-05-11T10:59:16.731Z")
        }
    ],
    "createdAt" : ISODate("2020-05-11T10:35:00.739Z"),
    "updatedAt" : ISODate("2020-06-01T13:47:08.993Z"),
    "__v" : 0
}

我想要这种类型的结果

{
    "_id" : ObjectId("5eda578cb9c3fa1ef8c483c5"),
    "brand_name" : "LAVA",
    "short_description" : "Specifications",
    "offer_price" : 20,
    "quantity" : 5,
    "variants" : [ 
        "5eb9300438d0b83a3088feec", 
        "5eb92a7909823240081cd763"
    ],
 "attributes" : [ 
        {
            "_id" : ObjectId("5eb92a5409823240081cd760"),
            "is_active" : true,
            "name" : "Size",
            "slug" : {
                "is_active" : true,
                "_id" : ObjectId("5eb9300438d0b83a3088feec"),
                "name" : "S",
                "updatedAt" : ISODate("2020-05-11T10:59:16.731Z"),
                "createdAt" : ISODate("2020-05-11T10:59:16.731Z")
            },
            "createdAt" : ISODate("2020-05-11T10:35:00.739Z"),
            "updatedAt" : ISODate("2020-06-01T13:47:08.993Z"),
            "__v" : 0
        }, 
        {
            "_id" : ObjectId("5eb92a4d09823240081cd75f"),
            "is_active" : true,
            "name" : "Color",
            "slug" : {
                "is_active" : true,
                "_id" : ObjectId("5eb92a7909823240081cd763"),
                "name" : "Green",
                "updatedAt" : ISODate("2020-06-01T13:47:56.584Z"),
                "createdAt" : ISODate("2020-05-11T10:35:37.357Z")
            },
            "createdAt" : ISODate("2020-05-11T10:34:53.642Z"),
            "updatedAt" : ISODate("2020-06-01T13:48:09.194Z"),
            "__v" : 0
        }
    ]
    "seller_id" : ObjectId("5e994af17a2f9b3a007e247c"),
    "name" : "LAVA X10",
    "createdAt" : ISODate("2020-06-05T14:32:44.582Z"),
    "updatedAt" : ISODate("2020-06-05T14:32:44.582Z")
}, 
{
    "_id" : ObjectId("5eda578cb9c3fa1ef8c483c8"),
    "brand_name" : "LAVA",
    "short_description" : "Specifications",
    "offer_price" : 20,
    "quantity" : 5,
    "variants" : [ 
        "5eb9300438d0b83a3088feed", 
        "5eb92a7909823240081cd763"
    ],
"attributes" : [ 
        {
            "_id" : ObjectId("5eb92a5409823240081cd760"),
            "is_active" : true,
            "name" : "Size",
            "slug" : {
                "is_active" : true,
                "_id" : ObjectId("5eb9300438d0b83a3088feed"),
                "name" : "XXL",
                "updatedAt" : ISODate("2020-06-01T13:46:36.417Z"),
                "createdAt" : ISODate("2020-05-11T10:59:16.731Z")
            },
            "createdAt" : ISODate("2020-05-11T10:35:00.739Z"),
            "updatedAt" : ISODate("2020-06-01T13:47:08.993Z"),
            "__v" : 0
        },  
        {
            "_id" : ObjectId("5eb92a4d09823240081cd75f"),
            "is_active" : true,
            "name" : "Color",
            "slug" : {
                "is_active" : true,
                "_id" : ObjectId("5eb92a7909823240081cd763"),
                "name" : "Green",
                "updatedAt" : ISODate("2020-06-01T13:47:56.584Z"),
                "createdAt" : ISODate("2020-05-11T10:35:37.357Z")
            },
            "createdAt" : ISODate("2020-05-11T10:34:53.642Z"),
            "updatedAt" : ISODate("2020-06-01T13:48:09.194Z"),
            "__v" : 0
        }
    ]
    "seller_id" : ObjectId("5e994af17a2f9b3a007e247c"),
    "name" : "LAVA X11",
    "createdAt" : ISODate("2020-06-05T14:32:44.582Z"),
    "updatedAt" : ISODate("2020-06-05T14:32:44.582Z")
}

// ------------这里的变量数组值是属性的子ID ----------------- 请帮我 请让我知道如何?

1 个答案:

答案 0 :(得分:0)

那不是非常有效的数据设计:您的一个馆藏使用了其他馆藏的文档的 parts 的ID。首先,您需要考虑为属性设置slug._id: 1索引。然后,要获取具有包含已过滤特性的属性的产品,请使用

db.product.find(...).map(function(prod){
  prod.attributes = [];
  for(sid in prod.variants){
    var attr = db.attributes.find({'slug._id': sid});
    attr.slug = attr.slug.find(function(slug){return slug._id == sid});
    prod.atributes.push(attr);
  }
  return prod;
})