MongoDB - 如何更新或插入数组中的对象

时间:2021-04-28 04:14:26

标签: reactjs mongodb express mongoose mongodb-query

我有以下收藏

{
        "likes": [],
        "_id": "6086f47a3e8c0411f0a66d22",
        "creator": "dimer",
        "picture": "",
        "title": "",
        "body": "hello world",
        "comments": [
            {
                "isReady": true,
                "likes": [],
                "_id": "6086fcf33e8c0411f0a66d25",
                "creatorId": "607e50a16e852544d41a1d9d",
                "creator": "dimer",
                "body": "hello worl nigger",
                "replies": [],
                "timestamp": 1619459315854
            },
        ],
        "createdAt": "2021-04-26T17:12:26.632Z",
        "updatedAt": "2021-04-27T04:22:28.159Z",
        "__v": 0
    },

如果评论和帖子存在,我想推入 comment.replies 一个新回复。 如何使用条件更新或插入对象到嵌套数组中?

我试过了:

module.exports.createReply = async (req, res) => {
  const user_ID = req.body.creatorId;
  const post_ID = req.params.id;
  const comment_ID = req.body.commentId;

  if (!ID.isValid(user_ID) && !ID.isValid(post_ID) && !ID.isValid(comment_ID)) {
    return res.status(400).send("ID unknown");
  }

  try {
    console.log("hello woorld");
    const reply = {
      creatorId: user_ID,
      creator: req.body.creator,
      body: req.body.body,
      timestamp: new Date().getTime(),
    };
    console.log("reply", reply);
    await PostModel.findById(post_ID, (err, docs) => {
      console.log(comment_ID);
      const comment = docs.comments.find((comment) =>
        comment._id.equals(comment_ID)
      );
      console.log("comment", comment);
      if (!comment) return res.status(404).send("comment not found" + err);
      comment.replies = [...comment.replies, reply];

      return docs.save((err, docs) => {
        if (!err) return res.status(200).send(docs);
        return res.status(400).send(err);
      });
    });
  } catch (error) {
    return res.status(400).send(err);
  }
};

我想我没有收到回复,因为我收到了这个错误:

{
    "errors": {
        "comments.4.creator": {
            "name": "ValidatorError",
            "message": "Path `creator` is required.",
            "properties": {
                "message": "Path `creator` is required.",
                "type": "required",
                "path": "creator"
            },
            "kind": "required",
            "path": "creator"
        }
    },
    "_message": "post validation failed",
    "name": "ValidationError",
    "message": "post validation failed: comments.4.creator: Path `creator` is required."
}

这是我的模型:

const nongoose = require("mongoose");
const PostSchema = nongoose.Schema(
  {
    creatorId: {
      type: String,
      // trim: true,
      // required: true,
    },
    creator: {
      type: String,
      trim: true,
      required: true,
    },
    title: {
      type: String,
      maxlength: 80,
    },
    body: {
      type: String,
      trim: true,
      maxlength: 250,
      required: true,
    },
    picture: {
      type: String,
    },
    video: {
      type: String,
    },
    likes: {
      type: [String],
      require: true,
    },
    comments: {
      required: true,
      type: [
        {
          isReady: {
            type: Boolean,
            default: true,
          },
          creatorId: {
            type: String,
            required: true,
          },
          creator: {
            type: String,
            required: true,
          },
          timestamp: Number,

          body: {
            type: String,
            required: true,
            trim: true,
          },
          likes: {
            type: [String],
            required: true,
          },
          replies: {
            require: true,
            type: [
              {
                isReady: {
                  type: Boolean,
                  default: true,
                },
                creatorId: {
                  type: String,
                  required: true,
                },
                creator: {
                  type: String,
                  required: true,
                },
                body: {
                  type: String,
                  required: true,
                  trim: true,
                },
                timestamp: Number,
              },
            ],
          },
        },
      ],
    },
  },
  {
    timestamps: true,
  }
);

module.exports = nongoose.model("post", PostSchema);

2 个答案:

答案 0 :(得分:0)

正如错误所说,路径 creator 是必需的。 确保回复包含“创建者”字段。

答案 1 :(得分:0)

要在更新的返回值中获取更新的文档,您需要使用 findOneAndUpdate 1 或 findAndModify 方法。这两种方法都有一个参数,您可以在其中指定返回更新的文档。请注意,Mongoose ODM 有相应的方法,但语法可能略有不同。

我的解决方案:

module.exports.createReply = async (req, res) => {
  const user_ID = req.body.creatorId;
  const post_ID = req.params.id;
  const comment_ID = req.body.commentId;

  if (!ID.isValid(user_ID) && !ID.isValid(post_ID) && !ID.isValid(comment_ID)) {
    return res.status(400).send("ID unknown");
  }

  try {
    const reply = {
      creatorId: user_ID,
      creator: req.body.creator,
      body: req.body.body,
      timestamp: new Date().getTime(),
    };
    const query = { _id: post_ID };
    const update = { $push: { "comments.$[elem].replies": reply } };
    const options = { new: true, arrayFilters: [{ "elem._id": comment_ID }] };
    await PostModel.findOneAndUpdate(query, update, options);
    let updated = await PostModel.findOne({ _id: post_ID });

    return res.status(200).send({
      data: updated.comments.find((comment) => comment._id.equals(comment_ID)),
    });
  } catch (err) {
    return res.status(400).send({ err: err });
  }
};
相关问题