这是我在模型中的架构。
const applicantEvaluationSchema = new mongoose.Schema(
{
applicantIdx: Number,
application: {
comments: [
{
userIdx: Number,
comment: String,
createdAt: Date,
},
],
evaluations: [
{
userIdx: Number,
point: Number,
},
],
},
interview: {
comments: [
{
userIdx: Number,
comment: String,
createdAt: Date,
},
],
evaluations: [
{
userIdx: Number,
point: Number,
},
],
},
}
);
我想在 application.comments 中推送评论
我想到了克隆数组,推送我的评论并更新它
但我认为有更好的方法来推动物体。
我该如何解决?
您可以Model.update
与$push
运算符一起使用:
//Your model
ApplicantEvaluation.update(
{ /* selection criteria */ },
{
$push: {
'application.comments': /* new comment object */
}
}
);
使用$push
运算符,您提供一个字段以推送到新对象。如果您有嵌套字段,请使用点语法访问该字段。由于comments
数组嵌套在application
use 中'application.comments'
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句