this._MyModel.findOneAndUpdate(
{ email },
{
$set: {
'tradeAccount': {
limitType,
limit,
}
},
$push: {
'tradeAccount.history': history
}
}
);
I want to add another object into an existing array using $push
but in the same time I am performing another update on the same object using $set
.
I know that I cannot use two operators on the same field such as ( $set
and $push
).
I wonder if there is another way to add another object into an existing array using $set
, or if there is any other way to do this in the same update request.
I was thinking of updating the array using $set
by retrieving the existing history and concatenate with the new one. But that requires an extra call to the db.
You can use
this._MyModel.findOneAndUpdate(
{ email },
{
$set: {
'tradeAccount'.'limitType': limitType,
'tradeAccount'.'limit': limit
},
$push: {
'tradeAccount.history': history
}
}
);
This should update the limitType and limit fields, and then push onto history. You aren't performing multiple queries on the same field this way because you aren't doing anything directly to 'tradeAccount'.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.