在嵌套子文档MongoDB中更新字段

时间:2019-06-27 12:55:39

标签: node.js mongodb mongoose nested subdocument

我正在尝试更新功能对象内的IsActive标志,但是它深深地嵌套在公司和工厂对象内。尝试使用$没有用,因为它不适用于深度嵌套的子文档。有没有人找到解决这个问题的方法?我希望IsActive标志会被修改,因为现在只能被修改。我尝试过:

  • $$

  • 列表项

  • 将$放置在各个点上,即工厂函数$ .IsActive -在functionIds中串联

{function deleteFunction(companyId, factoryId, functionId) {
    return Companies.update({
        "CompanyId": companyId,
        "Factories.FactoryId": factoryId,
        "Factories.Functions.FunctionId": functionId,
    }, {"$set": {"Factories.$.Functions.IsActive": false}}
    ).then(function (result)  {
        console.log("Reached", result);
        return result
    }).catch(function (err) {
        logger.log(err);
    });
}}

let functionsSchema = new mongoose.Schema({
    FunctionId: Number,
    Name: String,
    ADGroup: String,
    IsActive: Boolean,
    DateCreated: {
        type: Date,
        default: Date.now
    },
    DateModified: {
        type: Date,
        default: Date.now
    }
});

let factorySchema = new mongoose.Schema({
    FactoryId: Number,
    Name: String,
    ADGroup: String,
    IsActive: Boolean,
    DateCreated: {
        type: Date,
        default: Date.now
    },
    DateModified: {
        type: Date,
        default: Date.now
    },
    Functions: [functionsSchema]
});

let companySchema = new mongoose.Schema({
    CompanyId: Number,
    Name: String,
    IsActive: Boolean,
    DateCreated: {
        type: Date,
        default: Date.now
    },
    DateModified: {
        type: Date,
        default: Date.now
    },
    Employees: [employeeSchema],
    Factories: [factorySchema]

});

0 个答案:

没有答案