Reputation: 1433
Am I able to update a value in findOneAndUpdate
, with a other value from this document?
My data structure looks like
User: {
email: "[email protected]",
test: {
token: "someFancyToken",
email: "[email protected]"
}
}
How I wish to change the data
await UserModel.findOneAndUpdate(
{
'test.token': token,
},
{
email: test.email,
test: null
},
)
Output
User: {
email: "[email protected]",
test: null
}
Upvotes: 0
Views: 397
Reputation: 11975
Starting in MongoDB 4.2, you can use the aggregation pipeline for update operations so you can do something like this:
await UserModel.findOneAndUpdate(
{ 'test.token': token },
[{ $set: { email: "$test.email", test: null } }]
)
Upvotes: 2