Reputation: 307
after reading:
DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.
I've replaced collection.update by collection.updateOne that give me another result when pushing an array in an array.
I have tried to use findOneAndUpdate but I obtain the same result
my code before
Coupon.update({ "_id": couponId }, { $push: { "usersUsedCoupons": [userId, 1] } },
{ new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true }).exec(function (err, info) {
if (err) {
console.log(err);
} else {
console.log("info : ", info);
return info;
}
});
}
this was pushing [userId, 1] to the end of the array
my code after
Coupon.updateOne({ "_id": couponId }, { $push: { "usersUsedCoupons": [userId, 1] } },
{ new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true }).exec(function (err, info) {
if (err) {
console.log(err);
} else {
console.log("info : ", info);
return info;
}
});
now it push each index of the array [userId, 1] in a different index of parent array like:
0: userId,
1: 1
I expect to have the same behavior as Coupon.update
Upvotes: 0
Views: 786
Reputation: 307
I found the solution updateOne is pushing each index of the array in a different index in contrary with update so using update:
[userId, 1]
using updateOne:
[[userId, 1]]
UPDATE
After updating to mongoose ^6.3.6 now for exemple we push a single index
$push: {
status: {
date: new Date(),
branchId: dn.branchId,
branchName: dn.branchName,
actualStatus: dn.actualStatus,
},
}
for multiple index:
$push: {
status: {
$each: [
{
date: new Date(),
branchId: dn.branchId,
branchName: dn.branchName,
actualStatus: dn.actualStatus
},
{
date: new Date(),
branchId: dn.branchId,
branchName: dn.branchName,
actualStatus: dn.actualStatus
}
]
}
}
Upvotes: 1