首页 > 解决方案 > 可以使用 mongodb 查询更新文档,但在 mongoose 中执行时不起作用

问题描述

我的收藏是这样的:https ://mongoplayground.net/p/91InBXrUq7R

有了这个查询,我可以更新replies.likes

db.getCollection("posts").updateOne(

{
    "_id": ObjectId("5da832caeb173112348e509b"), //posts._id
    "comments.replies._id":ObjectId("5db6a88f7c6cfb0d0c2b689b"),//replies._id
  },
  { "$push": { "comments.$[outer].replies.$[inner].likes": "10000012" } },
  {
    "arrayFilters": [
      { "outer._id": ObjectId("5db06e11d0987d0aa2cd5593") },//comments._id
      { "inner._id": ObjectId("5db6a88f7c6cfb0d0c2b689b") }//replies._id
    ]
  }
)

但是当我使用猫鼬编码时,快递,集合不更新

    //Like Reply toggle
router.post("/toggleLikeReply", function(req, res, next) {
  var id_post = req.body.id_post;
  var id_comment = req.body.id_comment;
  var id_reply = req.body.id_reply;
  var id_user = req.user._id;
  console.log("id_post: "+id_post+" id_comment: "+id_comment+" id_reply: "+id_reply+" id_user: "+id_user);

  //todo
  Post.aggregate([
      { $match: {_id: ObjectId(id_post),"comments._id": ObjectId(id_comment)}},
      { $unwind: "$comments"},
      { $match: { "comments._id": ObjectId(id_comment)}},
      { $project: {"replies": "$comments.replies", _id: 0}},
      { $match: { "replies._id": ObjectId(id_reply)}},
      { $project: {"likes": "$replies.likes", _id: 0}}, 
    ]).exec((err, users_liked) => {
      var index = users_liked[0].likes[0].indexOf(id_user);
      console.log(users_liked[0].likes[0]);
      //todo
      if (index == -1) {
        const updatePost = async () => {
          try { 
            await Post.updateOne({
              _id: ObjectId(req.body.id_post), 
              "comments.replies._id": ObjectId(req.body.id_reply)},
              { $push: {"comments.$[outer].replies.$[inner].likes": ObjectId(req.user._id)} },
            {
              "arrayFilters": [
                { "outer._id": ObjectId(req.body.id_comment)  },
                { "inner._id": ObjectId(req.body.id_reply) }
              ]
            }
            );
          } catch (error) {
             console.log("error", error);
          }
        };
        updatePost().then(function(data) {res.send({  like: true, success: true})});

      }else{
        const updatePost = async () => {
          try { 
           await Post.updateOne({
          _id: ObjectId(req.body.id_post), 
          "comments.replies._id": ObjectId(req.body.id_reply)},
          { $pull: {"comments.$[outer].replies.$[inner].likes": ObjectId(req.user._id)} },
        {
          "arrayFilters": [
            { "outer._id": ObjectId(req.body.id_comment)  },
            { "inner._id": ObjectId(req.body.id_reply) }
          ]
        }
        );
          } catch (error) {
            console.log("", error);
          }
        };
        updatePost().then(function(data) {res.send({  like: false, success: true})});
      }


  })

});

我记录了所有的 id,就像我直接使用 mongo 查询一样。

id_post: 5da832caeb173112348e509b 
id_comment: 5db06e11d0987d0aa2cd5593 
id_reply: 5db6a88f7c6cfb0d0c2b689b 
id_user: 5da85558886aee13e4e7f044

我的代码使用猫鼬和快递有什么问题?

标签: mongodbexpressmongoose

解决方案


试试这个查询

var mongoose = require('mongoose');
const Schema = mongoose.Schema
const ObjectId = Schema.Types.ObjectId 
const updatePost = async () => {
          try { 
            await Post.updateOne({
              _id: ObjectId(req.body.id_post), 
              "comments.replies._id": ObjectId(req.body.id_reply)},
              { $push: {"comments.$[outer].replies.$[inner].likes": req.user._id} },
            {
              "arrayFilters": [
                { "outer._id": ObjectId(req.body.id_comment)  },
                { "inner._id": ObjectId(req.body.id_reply) }
              ]
            }
            );
          } catch (error) {
             console.log("error", error);
          }
        };
        updatePost().then(function(data) {res.send({  like: true, success: true})});

推荐阅读