Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mongodb更新数组仅适用于特定字段_Node.js_Json_Mongodb_Express - Fatal编程技术网

Node.js Mongodb更新数组仅适用于特定字段

Node.js Mongodb更新数组仅适用于特定字段,node.js,json,mongodb,express,Node.js,Json,Mongodb,Express,我有一个主题的json数组reorderList: const reorderList = [ { _id: '5e6b419c76a16d5c44d87132', order: 0 }, { _id: '5e6b41a276a16d5c44d87139', order: 1 }, { _id: '5e6b41a776a16d5c44d87140', order: 2 } ] 我的主题是这样的: var TopicSchema = new Schema({ to

我有一个主题的json数组reorderList

const reorderList = [
    { _id: '5e6b419c76a16d5c44d87132', order: 0 },
    { _id: '5e6b41a276a16d5c44d87139', order: 1 },
    { _id: '5e6b41a776a16d5c44d87140', order: 2 }
]
我的主题是这样的:

var TopicSchema = new Schema({
    topicTitle: String,
    topicQuestion: [
        {           
            questionTitle: String,
            answer: String,
            order: Number
        }
    ]
}
现在我想根据reorderList的\u id更新我的主题问题顺序

但以下语句将替换主题问题中的所有内容(例如,问题标题和答案将被删除)


如何基于reorderList更新它,并将原始数据保留在topicQuestion中?

您使用的模式设计得很糟糕。这里可以做的是创建另一个模式,
TopicQuestionSchema
,并为它所属的主题添加一个
ref

var TopicQuestionSchema = new Schema({          
    questionTitle: String,
    answer: String,
    order: Number,
    topic: {type: ObjectId, ref: 'Topic'} // the name of your model
}
这样,您仍然可以跟踪问题所属的主题,并且仍然能够轻松地更新顺序

var TopicQuestionSchema = new Schema({          
    questionTitle: String,
    answer: String,
    order: Number,
    topic: {type: ObjectId, ref: 'Topic'} // the name of your model
}