简体   繁体   English

猫鼬更新嵌入式文档

[英]Mongoose update embedded document

id: { type: String, required: true, unique: true, default: uuid.v1 },
    description: { type: String },
    period: [{
        id: { type: String, default: uuid.v1 },
        start: { type: Date, default: Date.now },
        due: { type: Date },
        dueWarnByHours: { type: Number, integer: true },
        newnessByHours: { type: Number, integer: true },
    }],

i have an embedded mongodb database document like this. 我有一个像这样的嵌入式mongodb数据库文档。 i tried updating it like the below one 我试图更新它像下面的一个

WorkItem.update({ description: req.body.description},{period.rank: 3}, function(err, req) {
        if (err) return console.error(err);
        console.dir(reqWorkItemId + "Successfully removed the workItem from the database");
    });

but it isn't working how to update the embedded child part period->rank using mongoose 但是如何使用猫鼬更新嵌入式子部分期->排名是行不通的

The following code will help you:- 以下代码将帮助您:-

var findQuery = { description: req.body.description, 'period.id' : someId};
WorkItem.update(findQuery,{$set:{'period.$.rank': 3}}, function(err, req) {
    if (err) return console.error(err);
    console.dir(reqWorkItemId + "Successfully removed the workItem from the database");
});

OR 要么

 WorkItem.update(findQuery,{$set:{'period.$.rank': 3}}, function(err, req) {
    if (err) return console.error(err);
    console.dir(reqWorkItemId + "Successfully removed the workItem from the database");
});

NOTE:- This will only update the first object of period array. 注意:-这只会更新周期数组的第一个对象。

WorkItem.update({ id: d }, { description: req.body.description, $set: { 'status.0.rank': req.body.status.rank } },
        function(err, numRowsAffected, raw) {
            if (err) return console.error(err);
            if (numRowsAffected > 0) {
                console.dir("reqWorkItemId" + "Successfully removed the workItem from the database");
            } else {
                console.log("fail");
                //res.send(500, { error: 'carrier not updated' });
            }
        });

This one works for me 这个对我有用

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

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