Mongoose保存()不更新数据库文档中数组中的值

gg0vcinb  于 2023-06-23  发布在  Go
关注(0)|答案(4)|浏览(140)

我试图使用GUI更新集合(units)中的文档,更新后我想更新集合(users)中的值(user.Units,这是一个Unit名称的数组)。如果数组长度只有1个元素,它会被更新,并显示在数据库中,一切都运行良好,但当数组有多个元素时,我尝试通过for循环更新它,它显示它被更新,但当我检查数据库时,它仍然没有更新。
我真的不明白为什么当我通过循环更新值时它不更新数据库。

整体编辑更新功能:-

edit_unit: function (req, res, next) {
    var Data = req.body;

    Client_data.Unit.findById(req.params.unitId, function (err, unit) {
        var error = false;
        if (err) {
            error = err;
        } else if (!unit) {
            error = "FATAL: unable to look up Unit #" + req.params.unitId;
        } else {

            switch(req.body.name) {
                case 'Icon':
                    var Icon = unit.Icon;

                    User.find({"Units":Icon}, function (err, users) {
                        if (err)
                        console.log(err);

                        users.forEach(function (u) {
                            if (u.Units.length > 1) {
                            for (var i = 0; i <= u.Units.length; i++) {
                               if(u.Units[i] == Icon) {
                                   u.Units[i] = req.body.value;
                               }
                            }
                            }
                            else {
                                u.Units = req.body.value;
                            }
                            u.save(u);
                        });
                    });
                    unit[req.body.name] = req.body.value;
                    break;
                case 'Description':
                    unit[req.body.name] = req.body.value;
                    break;
                default:
                    unit[req.body.name] = req.body.value;
                    break;
            }
            var data = JSON.stringify(req.body);
            unit.save();

            res.writeHead(200, {
                'Content-Length': data.length,
                'Content-Type':  'application/json'
            });
            res.end(data);
        }
    });
}

请求体:-

{ name: 'Icon',
  value: 'Health Utility 22c',
  pk: '5395ed107cd92dc40eaafb56' 
}

用户架构:-

var userSchema = mongoose.Schema({
UserName:     { type: String, required: true },
Password:     { type: String },
FirstName:    { type: String, required: true },
LastName:     { type: String, required: true },
CompanyName:  { type: String },
PhoneNumber:  { type: Number },
StartDate:    { type: Date,   required: true },
EndDate:      { type: Date,   required: true, default: new Date('9999-12-12')  },
ClientID:     { type: ObjectId, ref: 'Client', default: null },
DepartmentID: { type: ObjectId, ref: 'Department' },
ManagerID:    { type: ObjectId, ref: 'User', default: null},
Units:        [ { type: String, required: true } ],
UserList:      { type: Array, default:[]},
Access:    [{ type: String, enum: ['DEMO', 'USER','MANAGER','ADMINISTRATOR','OWNER']}],
Credentials:  { type: String },
AFTE:         { type: Number},
SessionID:    { type: String, default: null }
}, { safe: true });
xxhby3vn

xxhby3vn1#

可以通知mongoose数据集已经改变如下:

doc.markModified('pathToYourAttribute') 

来自文档http://mongoosejs.com/docs/schematypes.html

person.anything = { x: [3, 4, { y: "changed" }] }; 
person.markModified('anything');

希望能帮上忙!

gk7wooem

gk7wooem2#

参见this issue。解决这个问题的一种方法是不通过经典的数组Index方法更新数组。那就这么做

doc.array.set(index, value);

而不是

doc.array[index] = value;

也可以查看FAQ和doc以获得更多详细信息。

f0ofjuux

f0ofjuux3#

我用下面的语句替换了保存方法:

Client_data.Unit.updateOne({_id: unit._id},unit);
2lpgd968

2lpgd9684#

是的,我通过添加doc.markModified('fullName ')来解决这个问题等等

相关问题