【问题标题】:Mongo Query Array of Objects with ObjectIds带有 ObjectIds 的 Mongo 查询对象数组
【发布时间】:2021-07-04 15:20:01
【问题描述】:

我有以下用户模型:

const userSchema = new mongoose.Schema({
    name: {
        type: String,
        required: [true, 'Please tell us your name!'],
    },
    email: {
        type: String,
        required: [true, 'Please provide your email'],
        //unique: true,
        lowercase: true,
        validate: [validator.isEmail, 'Please provide a valid email'],
    },
    phoneNumber: { type: String, default: '' },
    photo: {
        type: String,
        default: 'default.jpg',
    },
    // roles: [{ type: mongoose.Schema.ObjectId, ref: 'Roles' }],
    roles: [String],
    password: {
        type: String,
        required: [true, 'Please provide a password'],
        minlength: 8,
        select: false,
    },
    passwordConfirm: {
        type: String,
        required: [true, 'Please confirm your password'],
        validate: {
            // This only works on CREATE and SAVE!!!
            validator: function (el) {
                return el === this.password
            },
            message: 'Passwords are not the same!',
        },
    },
    passwordChangedAt: { type: Date, select: false },
    passwordResetToken: { type: String, select: false },
    passwordResetExpires: { type: Date, select: false },
    active: {
        type: Boolean,
        default: true,
        // select: false,
    },

    admissionDate: Date,

    positions: [
        {
            branch: {
                //branch al qeu pertenece el horario
                type: mongoose.Schema.ObjectId,
                ref: 'Branch',
            },
            area: {
                //area al que pertenece el horario
                type: mongoose.Schema.ObjectId,
                ref: 'Area',
            },
            position: {
                //area al que pertenece el horario
                type: mongoose.Schema.ObjectId,
                ref: 'Position',
            },
            salary: { type: Number, default: '' },
        },
    ],
    
    
})

我有属性positions,它是一个对象数组。每个对象都有该用户所属的branchareaposition 的引用(这些是单独的模型)。

我正在尝试创建一个可以显示属于某个分支和区域的用户的查询,但到目前为止我还无法创建该查询:

我试过了:

{
  "positions": {
    "branch": "60691ada4a9943d8eb562874", 
    "area": "60691aea4a9943d8eb562875"
   }
}

尝试使用 $in 运算符、$all 运算符、$elemMatch 运算符,但到目前为止没有任何效果。

您能否帮我构建正确的查询以获取所需的元素。

非常感谢您。

编辑: 使用$elemMatch 测试过滤器:

它应该找到这个元素:

【问题讨论】:

    标签: mongodb mongoose nosql


    【解决方案1】:

    您可以使用$elemMatch 来匹配相同元素中的两个字段

    db.collection.find({
      positions: {
        $elemMatch: {
          branch: "60691ada4a9943d8eb562874",
          area: "60691aea4a9943d8eb562875"
        }
      }
    })
    

    Playground

    【讨论】:

    • 嗨 turivishal,我在 Compass 上尝试了该解决方案,但没有找到任何结果。我用 Compass 的结果测试更新了问题。
    • 找到了,感谢您的解决方案只需要更深入地挖掘,我必须为 Id 添加 ObjectId('id')。 { 职位:{ $elemMatch:{ 分支:ObjectId('6069fd648693cbe8ec5a6cff'),区域:ObjectId('60691aea4a9943d8eb562875') } } }
    • 是的,对于指南针,您必须将其转换为 ObjectId,我已根据您的架构为猫鼬提供了解决方案,您无需转换它,因为您已将类型指定为架构中的对象 ID。
    猜你喜欢
    • 2012-09-09
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 2013-07-20
    • 2014-08-29
    相关资源
    最近更新 更多