如何在值数组中查找所有具有属性值的元素-猫鼬

时间:2020-10-23 19:27:46

标签: mongoose mongodb-query mongoose-populate

我的项目中有一个UserModel,该用户模型在下面的结构中具有mobile_number属性

const userSchema = new Schema(
    {
        firstname: {
            type: String,
            text: true,
            maxlength: 50
        },
        lastname: {
            type: String,
            text: true,
            maxlength: 50
        },
        username: {
            type: String,
            text: true,
            maxlength: 50
        },
        last_login: {
            type: Date
        },
        dob: {
            type: Date
        },
        mobile_number: [{
            type: String,
            text: true,
            maxlength: 50
        }]
    })

我还有一个手机号码数组['08099999394','0809992224','08176674394']

如何使用此手机号码数组获取列表中具有手机号码(user.mobile_number)的用户列表,而无需运行for循环?

1 个答案:

答案 0 :(得分:0)

查询数据库时,可以检查用户号码是否为数组中的$$运算符之一。

Here is the official MongoDB documentation about this

或者如果您要查询具有数字(其数字不为null)的用户,则可以使用以下方法:

user.find({ mobile_number: { $ne: null } })
相关问题