我已经多次使用 updateOne
,但是这个特定的调用不起作用。
我已经尝试使用 ObjectId
,但我仍然可以使用这个字符串 userId 获取我的用户对象。
但是,如果我确实使用了 ObjectId
,则在将其用作 _id
的过滤器值时会出现此类型错误
2322: Type 'ObjectId' is not assignable to type 'Condition<string>'. Type 'ObjectId' has no properties in common with type 'QuerySelector<string | RegExp>'.
这是我现有的非常简单的代码。
// i do set the key like this
const key = "profile.name";
const result = await UserModel.updateOne({
_id: userId
}, {
$set: {
[key]: "new name"
}
}, {
overwrite: true // looked online that this may help, but this still doesn't do anything
});
console.log(result);
// { n: 1, nModified: 1, ok: 1 }
这不会更新我的用户:
> db.users.find().pretty();
{
"_id" : ObjectId("6053b530e9f654250a747b26"),
"profile" : {
"_id" : ObjectId("6053b530e9f654250a747b27"),
"name": "old name" // <<<< this should be "new name"
},
}
还 - 尽管数据库中的记录没有更新,但该模型的 updatedAt
时间发生了变化!但该领域并没有改变。
有人有什么想法吗?我的头撞在这张桌子上。我想不通
编辑:这可能是猫鼬中的错误,请参阅此问题:https://github.com/Automattic/mongoose/issues/10045