【问题标题】:$push into Array in Object in the document Mongoose$push into Array in Object 文档 Mongoose
【发布时间】:2018-09-16 19:30:38
【问题描述】:

我如何 $push 进入 mongoose 文档中 Object 中的数组?

架构如下所示

{
  rating: {
      usersRated: Array,
      rating: Number
  }
}

我试过{rating: {$push: {usersRated: data.userId}}},但还是不行。

【问题讨论】:

    标签: mongodb mongoose mongoose-schema


    【解决方案1】:

    你应该update the collection

    在你的情况下:

    model.update({ _id: id }, { $push: { 'rating.usersRated': data.userId }}, callback);
    

    更新时,你应该通过the operator before the fields

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 1970-01-01
      • 2022-12-01
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      • 2022-01-18
      相关资源
      最近更新 更多