如何使用聚合$ lookup和$ unwind在相同集合中嵌套mogodb结果

时间:2019-06-12 17:49:15

标签: javascript node.js mongodb mongoose aggregation-framework

我收藏了Product和Rateplan。我已使用MongoDB聚合展开和查找来获得所需的嵌套,但无法正确分组。

    productSchema = {
        name:{
            type:String,
        },
        baseprice:{
            type:Number,
            get:function(num) { return (num / 100).toFixed(2); },
            set:function(num) { return num * 100; }
        },
    }

    rateplanSchema = {
        startdate:{type:Date},
        enddate:{type:Date},
        name:{type:String},
        pricederived:{
            type:String,
            enum: ['NO','PP', 'MP', 'PN', 'MN'], 
            // Plus Minus
            // Percent Number
            // NO means dont apply derived and use strait price
        },
        price:{
            type:Number,
            get:num => (num / 100).toFixed(2),
            set:num => num * 100
        },
        productcategory:{
            type: mongoose.Schema.Types.ObjectId,
            ref: Category
        },
        products:[ {
            type: mongoose.Schema.Types.ObjectId,
            ref: Product
        }],
    }

我想要获得的结果是产品购买日期介于rateplans日期之间。

    Product: [{
                 name: "headphone"
                 baseprice: "20"
                 nestedplan: [
                             {
                                startdate: "15-04-2019"
                                enddate: "31-04-2019"
                                name: "Summer Sale"
                                pricederived: "MN"
                                price: "5"
                             }
                             ,
                             ,
                ]
              }
              ,
              ,
    ]

0 个答案:

没有答案