[英]Appending objects to array field using $push and $each returns null (Mongoose)
Background: I have an array (A) of new objects that need to be added to an array field in Mongoose. if successful it should print to screen the newly updated object but it prints null. I checked the database and confirmed it also does not update at all.背景:我有一个新对象数组 (A) 需要添加到 Mongoose 中的数组字段。如果成功,它应该打印以筛选新更新的 object,但它打印 null。我检查了数据库并确认它也没有完全更新。 I followed the docs to use the $push and $each modifiers here: https://www.mongodb.com/docs/manual/reference/operator/update/each/我按照文档在这里使用 $push 和 $each 修饰符: https://www.mongodb.com/docs/manual/reference/operator/update/each/
Desired Behaviour: I would like each object in (A) to be added to the Array field, not (A) itself.期望的行为:我希望将 (A) 中的每个 object 添加到数组字段,而不是 (A) 本身。 Upon success, it should print to screen the newly updated object.成功后,它应该打印以筛选新更新的 object。
Attempted Approach:尝试的方法:
let identifier={customer:{external_id:'12345'}}
let array = [{value:30},{value:30}]
User.findOneAndUpdate(identifier,{$push:{points:{$each:array}}},{new:true})
.then((result)=>{
console.log(result)
})
Attempted Resolutions:尝试的解决方案:
use dot "."使用点“。” notation for embedded field嵌入字段的符号
let filter={'customer.external_id':'12345'}
let array = [{value:30},{value:30}];
let update = { $push: { points: { $each: array } } };
User.findOneAndUpdate(filter,update,{new: true})
.then((result)=>{
console.log(result)
})
It turns out the IDs did not match.结果发现 ID 不匹配。 There was no issue with the code.代码没有问题。 Sorry guys.对不起大家。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.