【问题标题】:Mongoose find users with a field?Mongoose 用字段查找用户?
【发布时间】:2020-11-27 06:58:14
【问题描述】:

大家好!

我有一个非常简单的问题。这是我的架构:

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
    userid: {
        type: String,
        required: true
    },
    balance: {
        type: Number,
        default: 0,
        required: true
    },
    clearance: {
        type: Number,
        required: false
    }
})

module.exports = mongoose.model('User', userSchema)

我只想找到所有拥有该字段的用户。因此,无论实际人数如何,任何有任何许可的人都会被列入名单。我知道如何通过 id 或余额来查找用户,但我不知道如何仅根据字段来查找用户。

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    https://docs.mongodb.com/manual/reference/operator/query/exists/

    您可以使用$exists 运算符

    User.find({ 
        clearance: { $exists: true } 
    }).exec();
    

    请记住,这将包括许可无效的文件。

    【讨论】:

    • 应该没问题,只要我不给他们一个空权限 XD
    猜你喜欢
    • 2013-01-09
    • 2020-03-27
    • 2014-08-12
    • 2018-02-03
    • 1970-01-01
    • 2017-10-16
    • 2021-05-20
    • 1970-01-01
    • 2015-12-09
    相关资源
    最近更新 更多