阅读后:
DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead.
我用collection.updateOne替换了collection.update,当在数组中推入数组时,这给了我另一个结果。
我尝试使用findOneAndUpdate,但获得了相同的结果
我的代码
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;
}
});
}
这是将[userId,1]推到数组的末尾
我的代码
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;
}
});
现在它将数组[userId,1]的每个索引推送到父数组的另一个索引中 喜欢:
0:userId,
1:1
我希望与Coupon.update具有相同的行为
答案 0 :(得分:1)
我发现解决方案updateOne与更新相反地将数组的每个索引推入了不同的索引中,因此 使用更新:
[userId, 1]
使用updateOne:
[[userId, 1]]