推入数组猫鼬和蒙哥

时间:2019-06-06 04:19:42

标签: mongodb mongoose

每当我尝试插入嵌套在我的注释中的select create_date,count(*) as new_dts_num from dts where create_date = close_date group by create_date order by new_dts_num desc 数组时,都会收到以下错误Cannot create field 'likes' in element

执行以下操作时:

likeList

我的架构如下:

Feed架构

   Feed.findOneAndUpdate(
          {
            owner: req.body.authorId,
            "posts.comments.commentList._id": req.body.commentId
          },
          {
            $push: {
              "posts.$.comments.commentList.likes.likeList": {
                user: req.user._id,
                avatar: req.user.profile.profile_picture.url,
                name: req.user.name
              }
        )

我不确定在第一个参数中使用的查询是否存在问题。

更新整个错误消息 这很奇怪,因为看起来它实际上在寻找要访问的正确commentList,但是无法访问数组本身内的likes字段。我认为应该可以逐步解决吗? owner: { type: Schema.Types.ObjectId, ref: "userType" }, posts: [ { author: { userType: { type: String, enum: ["IndustryPartner", "User", "School"] }, user: { type: Schema.Types.ObjectId, ref: "posts.author.userType", //<- This may cause an issue, if there are any issues with retrieving user fields, CHECK THIS required: true }, name: { type: String, required: true }, avatar: { type: String, required: true } }, comments: { totalComments: { type: Number, default: 0 }, commentList: [ { likes: { totalLikes: { type: Number, default: 0 }, likeList: [ <---//Trying to push here { user: { type: Schema.Types.ObjectId, ref: "User" }, avatar: { type: String }, name: { type: String }, date: { type: Date, default: Date.now } } ] ...

posts.$.comments.commentList.likes.likeList

1 个答案:

答案 0 :(得分:0)

经过进一步的研究,在逐步遍历2个级别的数组后,似乎位置运算符不再有用。因此,解决方案是使用JS更改将值推入数组,然后保存它们。