为ChallengeSchema添加变量

问题描述 投票:0回答:1

我试着把喜欢不喜欢不满意变量添加到我的challengeemodel中,但当我查看我的数据库(Atlas MongoDB)时,我只能看到最初在那里的那些变量(标题,描述)。

我的数据库有一个集合 "挑战 "与4个挑战与下面的结构(不喜欢dislikessatisfaction)。我如何也包括likesdislikessatisfaction与他们的默认值0在这里?

  QUERY RESULTS 1-4 OF 4
    _id:5eb98c06a1f08159842b430d
    title:"aaaaaaa"
    description:"aaaaaaaaa"
    __v:0

const challengeSchema = new Schema({
    title: String,
    description: String,
    initiator: {type: Schema.Types.ObjectId, ref:"User"},
    likes: { type: Number, default: 0 },
    dislikes: { type: Number, default: 0 },
    satisfaction: { type: Number, default: 0 }
})

//add challenge
router.post("/startchallenge", (req,res) => {
  Challenge
  .create({
    title: req.body.title,
    description: req.body.description,
    likes: req.body.likes,
    dislikes: req.body.dislikes,
    satisfaction: req.body.satisfaction
  })
  .then((response) => {
    res.json(response)
  })
  .catch(error => {
    res.json(error)
  })
})
javascript mongodb mongoose-schema
1个回答
0
投票

MongoDb的设计是以这样的方式来操作文档,而文档只是你的数据的一个视图,如果你想让你的所有文档具有相同的结构,你必须手动更新已经存在于你的集合中的每个文档。

如果你的应用程序有这样的问题,你可能需要切换到SQL,它很容易携带这些问题,当然还有其他的折衷。

© www.soinside.com 2019 - 2024. All rights reserved.