【发布时间】:2018-11-10 18:57:40
【问题描述】:
我有以下架构
const ButtonSchema = new mongoose.Schema({
conditions: mongoose.Schema.Types.Mixed
})
// Data Model
const DataSchema = new mongoose.Schema({
buttons: [ButtonSchema],
__v: {type: Number, default: 0}
})
//Question Model
const QuestionSchema = new mongoose.Schema({
data: [DataSchema],
__v: {type: Number, default: 0}
})
// Dialog Model
const Dialog = mongoose.model('Dialog', new mongoose.Schema({
name: String,
questions: [QuestionSchema]
}))
所以本质上,对象是这样的
{
"download_data": [
{
"__v": 0,
"data": [
{
"text": [
"xxxx"
],
"__v": 0,
"buttons": [],
"_id": "5afdf6cf1c1cc542c4580511",
"conditions": {
"false": {
"type": "endDialog",
"data": [
{
"text": []
}
]
}
}
}
],
"_id": "5afdf6cf1c1cc542c4580510",
"type": "confirm"
},
{
"__v": 0,
"data": [
{
"text": [
"xxxx"
],
"__v": 0,
"buttons": [],
"_id": "5afdf6cf1c1cc542c458050f"
}
],
"_id": "5afdf6cf1c1cc542c458050e",
"type": "endDialog"
}
]}
如您所见,我有唯一的问题对象的 ID 和数据对象的 ID
在更新期间,我希望只通过 _id 更新特定的数据字段
我可以通过这个检索一个特定的问题
return DialogSchema.findOne({'questions._id': req.params.subId})
.then(function(data){
// this doesnt work
return data.questions[req.params.index].update(req.body)
})
.then(function(data){
console.log('data :', data)
})
.catch(function(err){
console.log('err: ', err)
})
但理想情况下,我只想检索 DataSchema 对象(作为数组对象插入 QuestionsSchema 对象中)
但这不起作用
return DialogSchema.findOne({'questions.data._id': req.params.subId})
.then(function(data){
console.log('data : ', data)
})
.catch(function(err){
console.log('err: ', err)
})
有什么办法可以做到吗?
【问题讨论】: