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

问题描述

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

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

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

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

解决方法

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

Here is the official MongoDB documentation about this

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

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...