简体   繁体   English

如何使用nodejs在MongoDB中增加数组object中的值?

[英]How to increment a value inside an array object in MongoDB using nodejs?

Here's my MongoDB post model that I am using with node.js.I want to update the number of likes under each object in comments array ie number of likes on each comment.How can I increment the number of likes value. Here's my MongoDB post model that I am using with node.js.I want to update the number of likes under each object in comments array ie number of likes on each comment.How can I increment the number of likes value.

_postid:6045b7a3b0b0423790d6484b
photo:Object
likes:Array
text:"hey there"
comments:Array
 0:Object
 1:Object
 2:Object
   _id :6045c9251f99b81ee4dbc0f6
    text:"tetstst"
    postedBy:6045c36dd8df2f2f00b115d5
    likes:0
    created:2021-03-08T06:50:13.851+00:00
created:2021-03-08T05:35:31.524+00:00
postedBy:6045116e37280f0970cf63a5

here's what I am trying to do using FindoneandUpdate:这是我尝试使用 FindoneandUpdate 做的事情:

Post.findOneAndUpdate(
    model,
    { $inc: { "comments.likes": 1 } },
    { new: true }
  ).exec((err, result) => {
    if (err) {
      return res.status(400).json({
        error: err,
      });
    }
    res.json(result);
  });
};

Here's my post schema that I am using:这是我正在使用的帖子架构:

text: {
    type: String,
    required: "Name is required",
  },

  photo: {
    data: Buffer,
    contentType: String,
  },
  likes: [{ type: mongoose.Schema.ObjectId, ref: "User" }],
  comments: [
    {
      text: String,
      created: { type: Date, default: Date.now },
      postedBy: { type: mongoose.Schema.ObjectId, ref: "User" },
      likes: Number,
    },
  ],

If you want to increment the likes for each comment by 1, (I assumed here model to be your query object.)如果您想将每条评论的点赞数增加 1,(我在这里假设model是您的查询 object。)

Post.findOneAndUpdate(
    model,
    { $inc: { "comments.$[].likes" : 1 } },
    { new: true }
  ).exec((err, result) => {
    if (err) {
      return res.status(400).json({
        error: err,
      });
    }
    res.json(result);
  });
};

If you want to increment only the first comment in the post,如果您只想增加帖子中的第一条评论,

Post.findOneAndUpdate(
    model,
    { $inc: { "comments.0.likes" : 1 } },
    { new: true }
  ).exec((err, result) => {
    if (err) {
      return res.status(400).json({
        error: err,
      });
    }
    res.json(result);
  });
};

If you want to increment likes for a comment posted By 6045c36dd8df2f2f00b115d5如果您想增加6045c36dd8df2f2f00b115d5发表的评论的点赞数

Post.findOneAndUpdate(
    { ...model, { "comments.postedBy": 6045c36dd8df2f2f00b115d5 }},
    { $inc: { "comments.$.likes" : 1 } },
    { new: true }
  ).exec((err, result) => {
    if (err) {
      return res.status(400).json({
        error: err,
      });
    }
    res.json(result);
  });
};

Also Ref: https://docs.mongodb.com/manual/reference/operator/update-array/另请参考: https://docs.mongodb.com/manual/reference/operator/update-array/

 await Post.findByIdAndUpdate(post.postId, {
    $inc: { like: 1 },
  });

if your like be announced number this code must work.如果您喜欢被宣布的号码,此代码必须有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM