【问题标题】:How to exclude one particular field from a collection in Mongoose?如何从 Mongoose 的集合中排除一个特定字段?
【发布时间】:2013-01-11 14:58:48
【问题描述】:

我有一个带有 Mongoose ODM(Mongoose 3.3.1) 的 NodeJS 应用程序。我想从我的集合中检索除 1 之外的所有字段。例如:我有一个包含 6 个字段的集合产品,我想选择除字段 "Image" 之外的所有字段。我使用了“exclude”方法,但出现错误.. 这是我的代码。

    var Query = models.Product.find();
    Query.exclude('title Image');

    if (req.params.id) {
        Query.where('_id', req.params.id);
    }


    Query.exec(function (err, product) {
        if (!err) {
            return res.send({ 'statusCode': 200, 'statusText': 'OK', 'data': product });
        } else {
            return res.send(500);
        }
    });

但这会返回错误

Express
500 TypeError: Object #<Query> has no method 'exclude'.........

我也试过了,var Query = models.Product.find().exclude('title','Image');var Query = models.Product.find({}).exclude('title','Image'); 但得到了同样的错误。如何从 Mongoose 的集合中排除一个/(两个)特定字段。

【问题讨论】:

    标签: javascript mongodb mongoose mongodb-query


    【解决方案1】:

    我将它与异步等待一起使用

    async (req, res) => {
          try { 
            await User.findById(req.user,'name email',(err, user) => {
              if(err || !user){
                return res.status(404)
              } else {
                return res.status(200).json({
                  user,
                });
              }
            });
          } catch (error) {
            console.log(error);
          }
    

    【讨论】:

      【解决方案2】:

      在 Mongoose 的更新版本中,您可以通过如下方式使用它来获取选定的字段。

      user.findById({_id: req.body.id}, 'username phno address').then(response => {
        res.status(200).json({
          result: true,
          details: response
        });
      }).catch(err => {
        res.status(500).json({ result: false });
      });
      

      【讨论】:

        【解决方案3】:

        你可以这样做

        const products = await Product.find().select(['-image'])
        

        【讨论】:

          【解决方案4】:
          Model.findOne({ _id: Your Id}, { password: 0, name: 0 }, function(err, user){
            // put your code
          });
          

          此代码在我的项目中有效。谢谢!!祝你有美好的一天。

          【讨论】:

            【解决方案5】:

            在当前 (3.x) Mongoose 构建中使用 query.select 进行字段选择。

            使用- 为要排除的字段名称添加前缀;所以在你的情况下:

            Query.select('-Image');
            

            顺便说一句:在 JavaScript 中,以大写字母开头的变量应该保留给构造函数。因此,请考虑在您的代码中将 Query 重命名为 query

            【讨论】:

            • 您也可以通过以下方式排除子文档字段:-Image.width
            【解决方案6】:

            我不知道你在哪里读到过那个 .exclude 函数,因为我在任何文档中都找不到它。

            但是你可以通过使用 find 方法的第二个参数来排除字段。

            这是来自the official documentation的示例:

            db.inventory.find( { type: 'food' }, { type:0 } )
            

            此操作返回type字段值为food的所有文档,但输出中不包含type字段。

            【讨论】:

            • 我在 [mongoosejs.com/docs/2.7.x/docs/query.html].正如上面的答案,我的 Image 字段对于不同的产品(每一行)可以有不同的值
            • 他在问猫鼬,你用的是原生驱动。
            • 救了我的命.. 删除 user.password 无法持续​​工作.. 害怕那样生活
            猜你喜欢
            • 1970-01-01
            • 2022-09-27
            • 1970-01-01
            • 2021-09-07
            • 2021-02-02
            • 2021-11-13
            • 2021-09-11
            • 1970-01-01
            相关资源
            最近更新 更多