【发布时间】:2020-08-03 07:57:36
【问题描述】:
我在地图集上有一个集群。我当前的对象如下所示:
{
"_id": {
"$oid":"5e9d8ccfb3d80e3824bf856c"
},
"user": {
"name":"test",
"channelGroups": [
{
"active": true,
"name": "an example of test",
"creationDate": "20 Apr 2020 at 13:35 PM",
"summary": "an example summary",
"channels": []
}
]
},
"__v": {
"$numberInt":"0"
}
}
这是我的控制器方法:(我使用猫鼬和快递)
createANewChannel: (req, res) => {
channelGroupModel.findOneAndUpdate({
"user.name": "test", "user.channelGroups": {
$elemMatch: {
"name": "an example of test" // req.params.slug
}
}
}, { $push: {
"channelGroups.channels": {
filter: {
keyword: "#example",
keywordType: "hashtag",
active: true,
fetchFrequency: 1,
},
tweets: []
}
}
},
(error, success) => {
if (error) {
console.log(error);
} else {
console.log(success);
}
}
);
res.send('ok');
},
问题: 我想根据 channelGroups 的 name 值将一个新的 channel 对象推送到 channelGroups 数组中。我的代码有效,但没有任何反应。
================================================ =======================
更新的工作版本:
channelGroupModel.findOneAndUpdate(
{
"user.name": "test"
},
{
$push: {
"user.channelGroups.$[elem].channels": {
filter: {
keyword: "#example",
keywordType: "hashtag",
active: true,
fetchFrequency: 1,
},
tweets: []
}
}
},
{
arrayFilters: [{"elem.name": "an example of test"}]
},
(error, success) => {
if (error) {
console.log(error);
} else {
console.log(success);
}
}
)
【问题讨论】:
标签: node.js mongodb express mongoose mongodb-query