MongoDB Mongoose 使用地图查找

MongoDB Mongoose find using Map

我有一个像这样的猫鼬模式:

const userSchema = new Schema( {
    email:{ type: String, required: true, unique:true, index:true },
    mobile:{ type: String },
    phone:{ type: String },
    firstname: { type: String },
    lastname: { type: String },
    profile_pic:{type:String},
    socialHandles: {
        type: Map,
        of: String
    },
    active: {type:Boolean, default:true}
}, {
    timestamps: true
} );

我想查询"give me user where socialHandles.instagram=jondoe"怎么办? 请帮助

Mongoose 的地图成为您数据库中的嵌套对象

{ "_id" : ObjectId(""), "socialHandles" : { "instagram": "jondoe" }, ..., "__v" : 0 }

因此您可以使用点符号查询它:

let user = await User.findOne({ 'socialHandles.instagram': 'jondoe' });