【问题标题】:How do I find all elements that have a property value in an array of value - Mongoose如何在值数组中找到所有具有属性值的元素 - Mongoose
【发布时间】:2021-02-06 21:13:12
【问题描述】:

我的项目中有一个 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']

使用这个手机号码数组,如何在不运行 for 循环的情况下获取在数组中拥有手机号码 (user.mobile_number) 的用户列表?

【问题讨论】:

    标签: mongoose mongodb-query mongoose-populate


    【解决方案1】:

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

    Here is the official MongoDB documentation about this

    或者如果你想查询有号码的用户(他们的号码不为空)你可以使用这个:

    user.find({ mobile_number: { $ne: null } })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 2016-05-29
      相关资源
      最近更新 更多