node.js - 如何将对象推送到 Mongoose 对象子数组
问题描述
const CampaignSchema = new Schema(
{
name: {
type: String,
required: true
},
userId: {
type: ObjectId,
ref: 'User'
},
liEmail: {
type: String,
required: true
},
type: {
type: String,
enum: campaignConstants.COMPAIGN_TYPES,
default: campaignConstants.CUSTOM
},
sequences: [
{
type: Sequence,
required: true
}
],
excludeOption: {
type: Boolean,
default: false
},
sourceLinks: [String],
data: [
{
targetId: {
type: ObjectId,
ref: 'Target'
},
profileLink: String,
name: String,
headline: String,
imageurl: String,
actions: [Activity]
}
],
browserKey: String
},
{
versionKey: false,
timestamps: true
}
)
我要将ACTIVITY对象推送到campaign->data->actions。这个ACTIVITY对象将通过带有 browserKey 的请求来。此browserKey和profielLink用于指示Campaign。和 profileLink 指示数据数组的特定对象
我自己试过这样但没有工作
const campaign = await Campaign.findOne({ browserKey })
const data = campaign.data.filter(
e => e.profileLink === target.profileLink
)
data.actions.push(action)
await campaign.save()
解决方案
推荐阅读
- jquery - 为什么 ajax 在移动设备上不像在桌面上那样工作?
- html - 将线性渐变添加到可滚动的 div
- android - 如何使用“Firestore”管道请求从同一集合中获取多个文档?
- python-2.7 - Boto3 + Python + AWS S3:如何从存储桶中的文件夹中获取最新文件
- kotlin - 如果模型为空,则不返回模型
- spring - 移动到目标系统之前的文件数据验证
- python - 将熊猫数据框转换为熊猫系列
- mongodb - 连接嵌套字段值(如果存在)
- android - onActivityResult() 将当前片段更改为上一个
- gstreamer - 我正在尝试在 VLC 媒体播放器上使用本地网络上的 gstreamer 流式传输 mp4 文件