【发布时间】:2021-09-17 07:29:10
【问题描述】:
我试图只删除我的 mongoDb 用户文档中的特定字段,但我认为使用 userModel.deleteOne({_id:id}) 是不够的,因为这会删除整个文档,这不是什么我想要。
{
"_id": "12345678" ,
"maidenName": null,
"createdAt": "2021-09-16T09:11:55.199Z" ,
"email": "287983@gmail.com",
"firstName": "iam1234",
"lastName": "test14",
"profile": "hello-world.com",
"uid": "SKkZZ3a",
"skills": []
}
我试过这样做UserModel.updateOne({_id:id},{$unset:{email: 1, firstName: 1}});
此查询返回
{
n: 0,
nModified: 0,
opTime: {
ts: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1631832363 },
t: 24
},
electionId: 7fffffff0000000000000018,
ok: 1,
'$clusterTime': {
clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1631832363 },
signature: { hash: [Binary], keyId: [Long] }
},
operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1631832363 }
}
我不知道这是什么!如何最好地解决这个问题?
【问题讨论】:
标签: mongodb mongoose mongoose-schema