node.js - Mongoose 更新嵌入文档

标签 node.js mongodb mongoose

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 },
    }],

我有一个像这样的嵌入式mongodb数据库文档。我尝试像下面那样更新它

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

但如何使用 Mongoose 更新嵌入的子部分 period->rank 不起作用

最佳答案

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

这个对我有用

关于node.js - Mongoose 更新嵌入文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39367658/

相关文章:

node.js - 如何使用 Node.js/Mongoose/MongoDB + ConnectSet 正确地进行负载平衡?

javascript - 使用 JavaScript 在网页之间共享数据

arrays - 用于存储子文档的 JSON 对象数组的 Mongoose 模式

node.js - mongodb 文档中子对象中的 _id(使用 Mongoose)

node.js - mongoose.mongo.MongoClient.connect() 是否在没有 mongo shell 的情况下工作?

javascript - 嵌套查询 mongoose .find

javascript - 如何使用 JWT 刷新 token 生成新的访问 token

node.js - MongoDB 正则表达式不一致

MongoDB $lookup 管道 : does this use indexes?

mongodb - Chef solo 错误: Cookbook apt not found