【问题标题】:MongoDB aggregation match nested arrayMongoDB聚合匹配嵌套数组
【发布时间】:2020-06-08 12:26:26
【问题描述】:

我正在努力从 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")
}

// ------------ 这里的变体数组值是属性的 slug ID ----- 请帮我 请告诉我怎么做?

【问题讨论】:

    标签: javascript node.js arrays mongodb


    【解决方案1】:

    这不是非常有效的数据设计:您的一个集合使用其他集合中文档的 parts 的 id。首先,您需要考虑为属性设置slug._id: 1 索引。然后要获取具有已过滤 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;
    })
    

    【讨论】:

    • 你能告诉我如何设计一个数据连接来解决这个问题,这样我就不需要使用 for 循环。您向我推荐了任何非常有用的数据收集设计
    猜你喜欢
    • 2017-07-26
    • 1970-01-01
    • 2014-03-04
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    相关资源
    最近更新 更多