mongodb不会从ObjectId数组中删除元素

时间:2020-05-21 10:53:40

标签: node.js mongodb express mongoose

我的架构如下:

const userSchema = new Schema({
  ...
  followings: [
      {
          user:{ 
              type: Schema.ObjectId, 
              ref: 'User' 
          },
      }

  ],
  followers: [
      {
          user:{ 
              type: Schema.ObjectId, 
              ref: 'User' 
          },
      }
  ],
}, {timestamps: true})

我需要实现“取消关注”功能。 我目前正在尝试:

try {

            // check if your id doesn't match the id of the user you want to unfollow
            if (user._id === current_id) {
                return res.status(400).json({ error: 'You cannot unfollow yourself' })
            }

            // remove the id of the user you want to unfollow from following array
            const query = {
                _id: current_id
            }

            const update = {
                $pull: { "followings": {"_id": user._id }}
            }

            const updated = User.update(query, update)

            // remove your id from the followers array of the user you want to unfollow
            const secondQuery = {
                _id: user._id
            }

            const secondUpdate = {
                $pull: { "followers": {"_id": current_id} }
            }

            const secondUpdated = User.update(secondQuery, secondUpdate)

            if (!updated || !secondUpdated) {
                return res.status(404).json({ error: 'Unable to unfollow that user' })
            }

            res.status(200).json({
                update, 
                secondUpdate
            })
} 
catch (err) {
            res.status(400).send({ error: err.message })
}

这将给出状态200,并将update和secondUpdate发送到客户端, 但实际对象不会从数据库中删除。 我的代码有什么问题?

2 个答案:

答案 0 :(得分:1)

如果您使用的是mongodb本机驱动器

您应该导入mongodb import cv2 import numpy as np import pytesseract import imutils img_rgb = cv2.imread('images/pd2.png') img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) template = cv2.imread('images/matchMe.png', 0) w, h = template.shape[::-1] res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED) threshold = 0.45 loc = np.where(res >= threshold) for pt in zip(*loc[::-1]): cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2) roi = img_rgb[pt, (pt[0] + w, pt[1] + h)] config = "-l eng --oem 1 --psm 7" text = pytesseract.image_to_string(roi, config=config) print(text) cv2.putText(img_rgb, text, (pt[0] + w, pt[1] + h), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 255), 3) cv2.imwrite('images/results.png', img_rgb) 。在执行操作之前。

ObjectID

更改为:

const ObjectId = require('mongodb').ObjectID;

$pull: { "followers": {"_id": current_id} }

答案 1 :(得分:0)

谢谢您的回答。 就我而言,以下对我有用。

try {

            // check if your id doesn't match the id of the user you want to unfollow
            if (user._id === current_id) {
                return res.status(400).json({ error: 'You cannot unfollow yourself' })
            }

            // remove the id of the user you want to unfollow from following array
            const query = {
                _id: current_id
            }

            const update = {
                $pull: { followings: {_id: user._id }}
            }

            const updated = User.updateOne(query, update, {
                safe: true
            }, function(err, obj) {
                console.log(err);
            })

            // remove your id from the followers array of the user you want to unfollow
            const secondQuery = {
                _id: user._id
            }

            const secondUpdate = {
                $pull: { followers: {_id: current_id} }
            }

            console.log(secondQuery)
            console.log(secondUpdate)

            User.updateOne(secondQuery, secondUpdate, {
                safe: true
              }, function(err, obj) {
                res.status(200).json({
                    obj
                });
              })

} 
catch (err) {
            res.status(400).json({ error: err.message })
}