【发布时间】:2017-10-22 05:38:54
【问题描述】:
在我的模型中,我有这样的东西:
subjects: {
Mathematics: {
questionsanswered: [
"x+2=3, solve for x please",
"How do you write an expression that represents all quadrantal angles?"
],
questionsasked: [
"how to convert sin to cos?",
"factor the trinomial: 3x^2+7x+2"
]
}
}
如您所见,有很多子元素,我完全是 Mongoose 和 Node.js 的初学者,我正在尝试在 questionsanswered 数组字段中添加另一个问题(字符串)。我查看了文档并尝试了
userModel.update({username: username},{$pushAll: { subjects:{Mathematics:{questionsasked:['what is the definition of calculus']}}}},{upsert:true},function(err){
if(err){
console.debug(err);
}else{
console.debug("Successfully added");
}
});
但是它说'Modifier $pushAll allowed for arrays only',有谁知道如何在questionsanswered 数组中插入另一个元素?非常感谢!
【问题讨论】:
-
试试
$pushAll: {"subjects.Mathematics.questionsasked": "what is the definition of calculus"} -
它仍然说同样的话:S
-
Nvm,我得到它使用 {"subjects.Mathematics.questionsasked": ["what is the definition of calculus"]} 只需要添加 ' [ ] ' 非常感谢!跨度>
标签: javascript node.js mongodb express mongoose