简体   繁体   English

MongoDB 聚合匹配嵌套数组

[英]MongoDB aggregation match nested array

I´m striving to get some aggregated data from Mongo DB.我正在努力从 Mongo DB 中获取一些汇总数据。 I have the following collections: I´m striving to get some aggregated data from Mongo DB.我有以下 collections:我正在努力从 Mongo DB 获取一些汇总数据。 I have the following collections: I have 2 collections:我有以下 collections: 我有 2 个 collections:

product产品

 {
        "_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. attribute 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
}

I want to this type of result我想要这种类型的结果

{
    "_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")
}

// ------------ Here variants array values are attribute's slug ID ----------------- Please Help Me Please let me know how? // ------------ 这里变体数组值是属性的 slug ID ----------------- 请帮助我 请让我知道如何?

That's not very effective data design: one of your collections uses ids of parts of documents in other collection.这不是非常有效的数据设计:您的 collections 之一使用其他集合中部分文档的 ID。 First of all you need to consider having slug._id: 1 index for attributes.首先,您需要考虑slug._id: 1属性索引。 Then to fetch products with included attributes which have filtered slugs use something like然后要获取具有已过滤 slug 的包含属性的产品,请使用类似

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;
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM