Node.js Mongodb更新数组仅适用于特定字段
我有一个主题的json数组reorderList: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
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
}