【发布时间】:2020-03-13 08:35:58
【问题描述】:
我有一个主题的 json 数组 reorderList:
const reorderList = [
{ _id: '5e6b419c76a16d5c44d87132', order: 0 },
{ _id: '5e6b41a276a16d5c44d87139', order: 1 },
{ _id: '5e6b41a776a16d5c44d87140', order: 2 }
]
而我的 TopicSchema 是这样的:
var TopicSchema = new Schema({
topicTitle: String,
topicQuestion: [
{
questionTitle: String,
answer: String,
order: Number
}
]
}
现在我想根据 reorderList 的 _id 更新我的主题问题 order。
但以下语句将替换 topicQuestion 中的所有内容(例如,questionTitle 和 answer 将被删除)
Topic.findOneAndUpdate(
{ '_id': topicId },
{ $set: { 'topicQuestion': reorderList } }, //replaces here
{ upsert: true },
function (err, response) {
...
});
如何根据reorderList进行更新,同时保留topicQuestion中的原始数据?
【问题讨论】:
标签: node.js json mongodb express