【问题标题】:Append/Add Objects without creating a new Parent in Mongoose追加/添加对象而不在 Mongoose 中创建新的父对象
【发布时间】:2015-06-16 21:16:00
【问题描述】:

我的架构如下

var DeptSchema   = new Schema({
    name : {type : String, default: ''},
    sku : {type : String, default: ''}, // (SKU = stock keeping unit)
    Product : {
    name : {type : String, default: '', unique:true},
    sku : {type : String, default: '', unique:true}, // (SKU = stock keeping unit)
    description : {type : String, default: '100gm'},
    price : {type : String, default: ''},
    quantity : {type : Number, default: '0'},
    isFav : {type : Boolean, default: 'false'}
    }
});

通过 Mongoose,我创建了一个 API,但是当我想将产品添加到特定部门(部门)时,问题就开始了,创建了一个全新的部门实例,而不是将新产品附加到现有部门。 我的 POST/PUT 如下所述是

.put(function(req, res) {
    // use our Dept model to find the Dept we want
    Dept.findById(req.params.Dept_id, function(err, Dept) {
        if (err)
            res.send(err);

        Dept.name = req.body.name;  // update the Dept info
        Dept.sku = req.body.sku;
        Dept.Product.name = req.body.ProductName;
        Dept.Product.sku = req.body.ProductSKU;
        Dept.Product.description = req.body.ProductDescription;
        Dept.Product.price = req.body.ProductPrice;
        Dept.Product.quantity = req.body.ProductQuantity;
        Dept.Product.isFav = req.body.ProductisFav;            
        // save the Dept
        Dept.save(function(err) {
            if (err)
                res.send(err);

            res.json({ message: 'Department updated!' });
        });
    });
})
.post(function(req, res) {

        var dept = new Dept();      // create a new instance of the Dept model
        dept.name = req.body.name;  // set the Dept name (comes from the request)
        dept.sku = req.body.sku;
        dept.Product.name = req.body.ProductName;
        dept.Product.sku = req.body.ProductSKU;
        dept.Product.description = req.body.ProductDescription;
        dept.Product.price = req.body.ProductPrice;
        dept.Product.quality = req.body.ProductQuality;
        dept.Product.isFav = req.body.ProductisFav;
        // save the Dept and check for errors
        dept.save(function(err) {
            if (err)
                res.send(err);

            res.json({ message: 'Department created!' });
        });        
    })

例如从输出中我们可以很容易地看到,不同的水果而不是附加到同一个水果部门正在创建一个完整的另一个实例。还有为什么 ProductSchema 没有自动生成的 Object Id?

[
{
"__v": 0,
"_id": "5528027cd4eb13d80cf81f87",
"Product": 
{
"isFav": true,
"quantity": 34,
"price": "128",
"description": "1kg",
"sku": "APL",
"name": "Apple"
},
"sku": "FRT",
"name": "Fruits"
},
{
"_id": "552824abd67bf9d81391ad92",
"__v": 0,
"Product": 
{
"isFav": true,
"quantity": 0,
"price": "40",
"description": "1kg",
"sku": "ORG",
"name": "Orange"
},
"sku": "FRT",
"name": "Fruits"
}
]

感谢您的耐心。

【问题讨论】:

    标签: node.js mongodb rest mongoose


    【解决方案1】:

    您已将 Product 声明为对象而不是数组。

    Product: {...} --> Product: [{...}]

    此外,您还需要更新 put 方法以将新项目推送到 Dept.Product 数组,而不是更新 Dept 的属性。您可以在 documentation 中阅读如何正确使用子文档。

    【讨论】:

    • 让我们使用子文档,Product = [ProductSchema]
      那么如何在部门内添加全新产品或修改现有产品。 #thanksforguiding
    猜你喜欢
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-10
    • 1970-01-01
    相关资源
    最近更新 更多