【问题标题】:Mongoose ignore specific value for object field in find()Mongoose 忽略 find() 中对象字段的特定值
【发布时间】:2021-08-01 04:50:26
【问题描述】:

我正在使用 mongoose 查询 MongoDB 中的用户帐户,它返回一个数组 具有相同姓氏和出生日期的用户。

但是它也返回当前用户的帐户,我怎样才能告诉 Mongoose 忽略具有特定 objectId 的帐户?

我需要 Mongoose 忽略 req.body.userId 对象字段 _id

谢谢

// CHECK FOR DUPLICATE USER ACCOUNT
router.post("/api/verification/check-for-duplicate-user", auth, async 
(req, res) => {

    try {
      const duplicates = await User.find(
        { DOB: req.body.DOB, lastName: req.body.lastName },
        function (err, result) {
          if (err) {
            console.log(`ERROR ${err}`)
          } else {
            console.log(`FOUND DUPLICATES`)
          }
        }
      )

      if (duplicates) {
        return res.send(duplicates)
      }
    } catch (err) {
      res.status(400).send()
    }
  }
)

【问题讨论】:

    标签: javascript express mongoose


    【解决方案1】:

    来自 mongodb 网站: 语法:{field: {$ne: value} }

    所以你需要这样的东西:

    User.find(
        { DOB: req.body.DOB, lastName: req.body.lastName, _id: {$ne: require('mongo').ObjectId(req.body.userId)} }
    

    【讨论】:

      猜你喜欢
      • 2019-04-05
      • 1970-01-01
      • 2011-02-24
      • 1970-01-01
      • 2020-03-07
      • 2021-11-20
      • 1970-01-01
      • 2012-02-01
      • 2017-04-17
      相关资源
      最近更新 更多